3

I have a question regarding complicated structures in Perl

my $data1 = [
  +{ id => 1, name => 'A' },
  +{ id => 2, name => 'B' },
  +{ id => 3, name => 'C' },
];

my $data3 = +{
   1 => +{ id => 1, name => 'A' },
   2 => +{ id => 2, name => 'B' },
   3 => +{ id => 3, name => 'C' },
};

How should I print "B"? What kind of data structure is that? And any nice reference on Perl nested structures (hash references, array references, etc.) that is eay to understand?

Thank you in advance

10
  • 1
    Do you know Data::Dumper ? Commented Mar 21, 2013 at 0:14
  • yes but im trying to do it without using modules Commented Mar 21, 2013 at 0:14
  • 3
    tutorial on data structures might be a good start for complex data structures like you have here. Commented Mar 21, 2013 at 0:17
  • 2
    I'm not sure what those + are doing in there?! Commented Mar 21, 2013 at 0:22
  • 5
    + disambiguates in places where the braces could be taken to be a code block instead of an anonymous hash reference, but one rarely needs that. I'm guessing the code contains them out of a misplaced desire to be clear and consistent with places where it could be ambiguous. Commented Mar 21, 2013 at 0:34

1 Answer 1

6

Try doing this :

print $data1->[1]->{name}; # ARRAY ref
print $data3->{2}->{name}; # HASH ref

This is de-reference from a perl ARRAY and HASH ref.

The -> de-reference explicitly. It's only needed for the first "floor", ex :

print $data1->[1]{name};
print $data3->{2}{name};

Works too. The 2nd and more are optionals.

Like Chris Charley said, take a look to the tutorial on data structures


To help you understanding what your scalar ref looks like, use Data::Dumper , ex :

print Dumper $data1;
print Dumper $data3;

Should output :

$VAR1 = [
          {
            'name' => 'A',
            'id' => 1
          },
          {
            'name' => 'B',
            'id' => 2
          },
          {
            'name' => 'C',
            'id' => 3
          }
        ];
$VAR1 = {
          '1' => {
                   'name' => 'A',
                   'id' => 1
                 },
          '3' => {
                   'name' => 'C',
                   'id' => 3
                 },
          '2' => {
                   'name' => 'B',
                   'id' => 2
                 }
        };

For the +{ } syntax, rra gives a good response :

disambiguates in places where the braces could be taken to be a code block instead of an anonymous hash reference, but one rarely needs that. I'm guessing the code contains them out of a misplaced desire to be clear and consistent with places where it could be ambiguous.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot! It does print now and by the answer you gave I somehow got an idea of what kind of thingies these are
Note that you don't always have to be explicit about dereferencing. After the first ->, using more of them to dereference is optional - $data1->[1]{name} is equivalent to $data1->[1]->{name}.
Btw, I really dislike perldsc, its a crutch giving recipes for each situation. To really understand references I like to point people to perldoc perlreftut which makes it nice and clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.