A longshot... is anyone familiar with nested data structures in Perl?

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
I'm confused by how references work in Perl. If I need to use them or not and so on.
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
I use references and very complex data structures all the time. What do you want to know?

my $hashref = { a => 1, b => 2};
print $hashref->{a}; # prints 1

my $arrayref = [ 1, 2, 3];
print $arrayref->[0]; # prints 1

my $hashOfArraysRef = { a => [1, 2, 3], b => [4, 5, 6] };
print $hashOfArraysRef->{a}[0]; # prints 1
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
I have a data structure that is several layers of nested lists and hashes.

So far I have a starting list that contains a hash table in each element. The key to that hash is a string, the value is another list.

How do I access the list at the end? I am trying to, but get "ARRAY 0x...".


Also, do you know of any beginner oriented tutorials on perl data structures?

Thanks - Justin
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
If you are getting ARRAY 0x... whatever that means that you are trying to print the reference itself, and not what that reference is pointing to. Without your exact data structure, I can't give you the exact syntax but to dereference your array ref as an array, put an @ on the front.

Example:

my @array = (1, 2, 3);
my $arrayref = \@array;
print $arrayref, "\n"; # prints "ARRAY(0x...)"
print @$arrayref, "\n"; # prints "123"

to access a specific element in the array you can do: $arrayref->[0]

my @sameExactArrayAsBefore = @$arrayref; # putting the @ sign in front of $arrayref says that I want to dereference the reference $arrayref as an array

I'm not sure what your programming experience is, but a reference is basically a pointer. The arrow (->) operator is the dereferencing operator which is used to "follow" the pointer to the thing it's pointing to. If you try to print the reference itself, you get that funny ARRAY(0x...) or HASH(0x...) or SCALAR(0x...) depending on what that reference is pointing to.

The Perl documentation is very good. Type 'perldoc perlref' or 'perldoc perlreftut' at the command line for some good walkthroughs on references. 'perldoc perldata' will give you everything you need to know about data structures. And for completeness, 'perldoc perl' will give you a list of all the built in perldocs.

If you have any specific questions, you will have to give us specfics. Preferrably post code you are having trouble understanding or give us the exact makeup of your data structure you are having trouble accessing.


 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
This is my data structure:

$player_info[$count]{$1} = ["A", "B", 0, 0, 0];

"player_info" is a list with hash tables in the elements. the key to the hash is "$1". The value of the hash is the array shown.

I got the array printing thanks to your help. :)

Now I need to have where strings "A" and "B" are in the value array be yet another set of hash tables. I am stuck once again trying to figure that out. They need to be empty hash tables for now, that I can add key/value pairs to as I process data.

Thanks again!
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
$player_info[$count]{$1} = [{}, {}, 0, 0, 0]; # Now elements "A" and "B" will be empty hash references. Note, reference to a hash, not a hash itself

or if you can't initialize it like this, you can do:

$player_info[$count]{$1}[0] = {};

To add new keys:

$player_info[$count]{$1}[0]{"newkey"} = "newvalue";

I think one of the reasons you are getting confused is because you are mixing non-references with references. Come to think of it, I can't remember that last time I used non-references. Everything I do at the enterprise level is with references. If you asked me, I would make that data structure like the attached but it doesn't really matter. References are fun!!

By the way, Data::Dumper and good code indenting are key to visualizing your data structures.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Thanks!

What are the benefits of using strict other than it makes you formally declare variables?
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
EDIT: I figured out what I asked here.:)

Yes I am getting mixed up on Perl references. I understand the concept, but not the Perl syntax. Our professor said they were an advanced topic that we won't be covering, but our project needs them for nested data structures.

 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: Davegod75
strict just keeps you "honest" when you are programming...i would always use it


Is it worthwhile for me to go over a ~500+ line program to use strict?
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Originally posted by: Jumpem
Originally posted by: Davegod75
strict just keeps you "honest" when you are programming...i would always use it


Is it worthwhile for me to go over a ~500+ line program to use strict?


ALWAYS use strict. Helps you avoid typos and a million other little problems. In the enterprise world it is absolutely mandatory. I can't believe your professor doesn't mandate it for all Perl programs. Going back and retrofitting your program with 'use strict' will probably only effect you in that you have to predeclare your variables with 'my' instead of just using them without declaring them. If you want to have any sort of future in professional Perl programming, you should get used to using strict now. That being said, if this is just a throwaway programming assignment and your professor doesn't care, it may not be worth it, but it will definitely save you some headaches in development. And while you're at it, 'use warnings' and 'use diagnostics' are also very helpful tools during the development phase. 'use diagnostics' adds a lot of bloat though so take that out before production.

Yes, references are more of an advanced topic but are incredibly useful. For a great book on all these advanced topics, get Advanced Perl Programming by Sriram Srinivasan
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
What sort of stuff do you do professionally in Perl. Are you working in bioinformatics?
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Originally posted by: Jumpem
What sort of stuff do you do professionally in Perl. Are you working in bioinformatics?

No, but I did some of that in college. Currently working in test automation. Perl front-ends, COM/RPC back-ends although at the moment my main project is in C#. You would probably be surprised at how much Perl pops up in the business world.