Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Not understanding the arrow operator

by bradcathey (Prior)
on May 18, 2009 at 19:54 UTC ( #764714=perlquestion: print w/ replies, xml ) Need Help??
bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians,

I couldn't get this map to work until I added the arrow operator, which I though was just for class methods and deferencing. Can someone explain what is going on with the arrow in this context, when nothing is being dereferenced AFAIK. Thanks.

my @pages = ( {'level' => '1','id' => 4}, {'level' => '2','id' => 10} ); my @ids = map { $_->{'id'} } @pages; print Dumper (@id); __END__ $VAR1 = 4; $VAR2 = 10;
—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Comment on Not understanding the arrow operator
Select or Download Code
Re: Not understanding the arrow operator
by John M. Dlugosz (Monsignor) on May 18, 2009 at 19:59 UTC
    Update: Sorry, I addressed the wrong point. I thought I read "fat arrow". But my note on not needing the quotes still holds.

    The fat arrow is the same as comma. It has the side effect of letting you not quote the left side. So

    'level' , '1','id' , 4
    should be exactly the same as
    'level' , '1','id' , 4
    as the hash constructor syntax (putting stuff between braces) just wants a list with an even number of elements, taken as alternating keys and values. That's why it works for parameter lists too.

    You could actually write

    level => '1', id => 4
    and be even less noisy.

    —John

Re: Not understanding the arrow operator
by kennethk (Monsignor) on May 18, 2009 at 20:02 UTC
    Oh, but something is being dereferenced. An array of hashes is actually an array of hash references, and so each element fed into your map is a scalar hash reference. You are probably confused because you can say $pages[0]{id}, which contains an implicit arrow operator (i.e. is equivalent to $pages[0]->{id}). See Using References for some details.

      Thanks kenneth and DStaal. I didn't realize that an AOH was really an array of hash refs with the implicit arrow. Interesting.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Not understanding the arrow operator
by DStaal (Chaplain) on May 18, 2009 at 20:05 UTC

    You are dereferencing: You have an array of hashes (or, more correctly an array of references to hashes) in @pages, so $_ in the map is a reference to a hash.

    This is partially hidden by the normal syntax, because perl is smart enough to realise when you type $pages[1]{id} you mean $pages[1]->{id}. (Because there is no other way element 1 of @pages could be a hash: Arrays can only hold scalars, and they only way to get a hash into a scalar is to use a reference.)

Re: Not understanding the arrow operator
by morgon (Deacon) on May 18, 2009 at 20:27 UTC
    explain what is going on with the arrow in this context, when nothing is being dereferenced
    As already explained above, you are in fact dereferencing $_ which in the context of your map is a reference to a hash.

    the arrow operator, which I though was just for class methods and deferencing
    Here's a summary to avoid total confusion:

    There are 3 cases where the array dereferences:

    - $arrayref->[0];
    - $hashref->{"key"};
    - $coderef->("argument");

    Then there are two cases for method calls:

    - $object->method; # passes $object as first argument
    - SomePackage->method; # passes "SomePackage" as first argument

    hth

      There are 3 cases where the array dereferences:
      Of your three examples, only the first one has anything to do with arrays.
        Hardly any dereference between arrow and array :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://764714]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-08-22 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (157 votes), past polls