Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Accessing an AoHoAoH

by bradcathey (Prior)
on Jun 05, 2004 at 19:32 UTC ( [id://361658]=note: print w/replies, xml ) Need Help??


in reply to Re: Accessing an AoHoAoH
in thread Accessing an AoHoAoH

Thanks calin, that did it! I guess I'm still a little confused about when to use parens and when to use brackets. In reading The Perl Cookbook and Programming Perl, it appears that I might have been confusing the building of a HoA and an AoH, the former using the brackets, and the latter using parens.

—Brad
"A little yeast leavens the whole dough."

Replies are listed 'Best First'.
Re^3: Accessing an AoHoAoH
by jeffa (Bishop) on Jun 05, 2004 at 19:46 UTC

    As a "i just now made this up rule of thumb", use braces when parens won't do. Parens are just a fragile container for lists, they will flatten by default:

    my @one_d = ( (1,2,3), (4,5,6), (7,8,9) );
    is really just a single list. You have to use braces:
    my @two_d = ( [1,2,3], [4,5,6], [7,8,9] );
    I prefer to use a reference to an anonymous array for the outside container, maily because there is no mixing of parens and braces, square or curly:
    my $two_d = [ [1,2,3], [4,5,6], [7,8,9] ];
    When in doubt, always consult Data::Dumper. Always! If you had used Data::Dumper on your data structure, you would have seen that the problem was within it:
    print Dumper \@AoH; __END__ (it's an AoHoH ... not an AoHoAoH) $VAR1 = [ { 'page' => { 'paragraph' => 'lesson1' }, 'chapter' => 'Basic', 'HASH(0x8638dec)' => undef }, { 'HASH(0x86fee04)' => undef, 'page' => { 'paragraph' => 'lesson3' }, 'chapter' => 'Advanced' } ];
    Finally, print out a copy of References quick reference. And don't forget about Data::Dumper! :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Great rule of thumb, jeffa. BTW, you have this knack for catching me with my zipper down. I always use Data::Dumper...er...almost all time. Thanks for the reminder to use it all the time and for the Quick Ref.

      —Brad
      "A little yeast leavens the whole dough."
      As a "i just now made this up rule of thumb", use braces when parens won't do.

      I think I'd rather the rot was "try braces when parens didn't do what you thought they would".

      Ditto for curlies and parens when dealing with hashes. Although looking at why, maybe with Data::Dumper, as you say, or looking at what perl told us when we tried to run the thing!!!, is always best. Otherwise we might as well be monkeys bashing away at our keyboards.
Re^3: Accessing an AoHoAoH
by bart (Canon) on Jun 06, 2004 at 16:36 UTC
    I guess I'm still a little confused about when to use parens and when to use brackets.
    Parens build a list, brackets build an anonymous array, and return a reference to it. Hashes can only contain scalars as values, so a list won't ever do (except when it contains only one item — but then you don't need a list). An array reference is a scalar, so that will work. As it's the only thing that actually does work properly, Perl's syntax has been optimized to ease access to array references as hash and array values.

    $x->{'foo'}[1] actually means: use $x as a hash reference, get the value associated with the string 'foo'. Use this as an array reference and access the second element (with index 1) from it.

    Actually you can even split this up into:

    $aref = $x->{'foo'}; $aref->[1]
    provided you didn't need autovivification, which were to happen in case $x->{'foo'} was undefined, in the former case — but not in the latter.
Re^3: Accessing an AoHoAoH
by Roy Johnson (Monsignor) on Jun 06, 2004 at 18:58 UTC
    If you want to nest something, use brackets. Nested parentheses are meaningless in lists; only the outermost set defines any structure.

    The PerlMonk tr/// Advocate

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://361658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-18 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found