Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks.

I was reading some code this afternoon:
#!/usr/bin/perl use warnings; my @staff = ( { age => 21, position => 'programmer', }, { age => 23, position => 'administrative assistant', }, );

Ok. I understand that the 'staff' array contains two anonymous hashes (indexed as '0' and '1' respectively). If I wanted to obtain the value of the 'age' key in the second hash, I could write:
print $staff[1]->{age};


I decided to experiment with this code slightly and enclose each anonymous hash within an anonymous array:
my @staff = ( [{ age => 21, position => 'programmer', }], [{ age => 23, position => 'administrative assistant', }], );

After doing so, I wasn't sure how to describe such a structure nor how to obtain the values stored within each respective hash.

Any help?

Thank you monks,

Linda

Replies are listed 'Best First'.
Re: Seemingly enigmatic data structures
by lidden (Curate) on Mar 05, 2005 at 05:19 UTC
    You get anonymous arrays with one element each. The following two do the same.
    print $staff[0][0]{age}, "\n"; print $staff[0]->[0]->{age}, "\n";
Re: Seemingly enigmatic data structures
by Prior Nacre V (Hermit) on Mar 05, 2005 at 06:34 UTC

    Linda,

    In this context using arrays and hashes, the -> operator:

    • must be used in one place
    • must not be used in one place
    • may be used everywhere else
    must

    It must be used to access the first data structure level of a reference to an array:

    my $ra_staff = \@staff; $ra_staff->[0]...
    must not

    It must not be used to access the first data structure level of an array:

    $staff[0]...
    may

    It is optional everywhere else: as shown by lidden above.

    Regards,

    PN5

      • must be used in one place
      • must not be used in one place
      isn't this a contradiction?

      UPDATE: Ah, OK, I get it now, more or less. Though I do think that could have been more clearly worded.

      "...first data structure level of a reference" is pretty gibberishy to me. But the examples make it more or less clear.

Re: Seemingly enigmatic data structures
by sh1tn (Priest) on Mar 05, 2005 at 13:37 UTC
    In addition:
    # in order to see stucture: use Data::Dumper; my @staff = ( [{ age => 21, position => 'programmer', }], [{ age => 23, position => 'administrative assistant', }], ); print Dumper(@staff);