Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Accessing an AoHoAoH

by calin (Deacon)
on Jun 05, 2004 at 19:07 UTC ( #361654=note: print w/replies, xml ) Need Help??


in reply to Accessing an AoHoAoH

use [ ... ] instead of ( ... )

Replies are listed 'Best First'.
Re^2: Accessing an AoHoAoH
by Happy-the-monk (Canon) on Jun 05, 2004 at 19:27 UTC

    use [ ... ] instead of ( ... )

    right. It's not a simple array but a reference to a nameless array.
    See also   perldoc perllol,   perldoc perlreftut,   perldoc perlref   and   perldoc perldata   for more syntactical details (the usual suspects =))

      It's not a simple array but a reference to a nameless array

      Just to be exact, it's not a "simple array" either. It's a list, which is not quite the same as an array. You can read about the differences by running perldoc -q "a list and" at a command prompt, or by viewing an online copy.

Re^2: Accessing an AoHoAoH
by robartes (Priest) on Jun 05, 2004 at 19:32 UTC

    Just to make things clear (well, it had me stumped for awhile), calin means replacing the () in the values of your page hashes by []. That way, you're assigning an anonymous array to the value of the hash, instead of a list, as you are currently doing.

    BTW, the @AoH assignment throws a warning:

    Odd number of elements in anonymous hash at aoh.pl line 21.

    That is because of the parentheses instead of square brackets.

    As to your second question: it's an array of hashes one of whose values is an array of hashes. So, in a sense, it is a AoHoAoH :)

    CU
    Robartes-

      Thanks robartes, for the explanation. I had changed all the parens to brackets and got warnings (after you reminded me to turn those on, duh). Anyway got it to work using the brackets as you indicated.

      —Brad
      "A little yeast leavens the whole dough."
Re^2: Accessing an AoHoAoH
by bradcathey (Prior) on Jun 05, 2004 at 19:32 UTC
    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."

      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.
      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.
      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://361654]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-09-26 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (117 votes). Check out past polls.

    Notices?