in reply to Perl style... help me figure this out.

May I ask why you're trying to steer clear from arrays (I'm assuming you mean loops instead of arrays) and grep?

Also, I'm pretty sure the code you provided does not run. Your $hash hashref has keys that point to arrayrefs, so you would need to dereference them like so:

print $hash->{COOKIES}->[0]; # Gives you: CHOCOLATE CHIP

EDIT: I stand corrected. $hash->{COOKIES}[0] works just fine. Seems weird to me, but there you go.

Replies are listed 'Best First'.
Re^2: Perl style... help me figure this out.
by Jenda (Abbot) on Feb 07, 2012 at 14:29 UTC

    That's quite simple. If the only thing you could ever write between the curly and the square bracket is a -> and you have to write it always, then why should you have to write it at all?

    It's a convenient shortcut that may be used no matter whether you are chaining array or hash subscriptions. $AoA[1][4], $HoH{some}{thing}, $AoH[1]{whatever}, $HoA{barf}[42], ...

    The arrow at the beginning though is important. $A[0] is the first element of the array @A, $A->[0] on the other hand is the first element of the array referenced by $A. Because arrays and hashes may only store scalars (strings, numbers or references) you do not need the arrow between the subscriptions to distinguish such two cases.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Ah, that makes sense. Thanks for enlightening me! It always amazes me when I learn about small things like this that make Perl so easy to use.