in reply to Re: Brief Hash Tutorial
in thread Brief Hash Tutorial

Although I'd recommend using further arrows other wise you'll get your knickers in a twist.

Remember that % means a whole hash, and $ is a hash value (as it is in effect a scalar).

So if $hashref contained a hash, then you could access the whole hash with %$hashref (the hash % in the scalar reference $hashref).

Remember references (or pointers if you will) are a single value (a single reference), so effectively a scalar, hence the $.

Use arrow notation and the appropriate brackets to access the nested hash/array you need.
$hashref = \%hash; # $hashref is a scalar pointing to %hash %$hashref or %{$hashref}# the same as %hash $hash{'key'} # is the same as $hashref->{'key'} $hashref->{'key'} = \@array; # Make this key a reference to the array +@array @{$hashref->{'key'}} # the same as @array, the extra brackets {} show +you are looking for the array in $hashref->{'key'} and not an array i +n $hashref $hashref->{'key'}->[0] # same as $array[0]
It takes a little getting used to, but all makes sense after a while.
The books Advanced Perl Programming (First Edition) and Intermediate Perl cover this quite well


Lyle

Replies are listed 'Best First'.
Re^3: Brief Hash Tutorial
by dreadpiratepeter (Priest) on Aug 31, 2008 at 13:30 UTC
    Actually, i disagree on the point of keeping the arrows in. Since everything after the first level is always a reference, the extra arrows are just line noise. It's just as easy to remember never to put them, rather than always to.
    Also, that frees you up to concentrate on the real issue of whether your top level access is by reference or not.
    So all the -> rules essentially (for simple to moderate cases) boil down to ref = -> at the top, hash (or list, etc) = no ->
    And as a side note to another point you made, I have started following Damian's advice about the deref and have been writing:
    %{$hashref}
    instead of:
    %$hashref
    it seems a little more readable, especially with a little whitespace:
    %{ $hashref }


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re^3: Brief Hash Tutorial
by gw1500se (Beadle) on Aug 31, 2008 at 02:40 UTC
    Great explanation. I learn best with examples and yours helped a lot.
Re^3: Brief Hash Tutorial
by JadeNB (Chaplain) on Sep 01, 2008 at 01:19 UTC
    So if $hashref contained a hash, then you could access the whole hash with %$hashref (the hash % in the scalar reference $hashref).
    Aside from your advice to leave in all de-referencing arrows, which can be good or bad depending on the context—for example, it can make it clearer to a new user what's going on, but it also completely destroys the often-intended resemblance to a multi-dimensional array (or hash)—I think most of your post is a good description; but you probably didn't say what you meant above. Of course the point is that $hashref doesn't contain a hash (or else it would be %hash or something), but rather that it contains (or perhaps just is) a reference to a hash.