Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

•Re: Re: RE: multiple keys - one value

by merlyn (Sage)
on Dec 04, 2003 at 21:17 UTC ( [id://312325]=note: print w/replies, xml ) Need Help??


in reply to Re: RE: multiple keys - one value
in thread using a hash of functions

It'd help if you explained what parts you already do understand, so we don't have to repeat that. Do you know what qw() does? How about map? References? sub{}?

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Re: RE: multiple keys - one value
by bradcathey (Prior) on Dec 05, 2003 at 02:14 UTC
    Okay, now I'm nervous ;-) My reason for asking is purely academic--I'm trying to REALLY learn Perl (yes I have read the Llama book and am plowing through the Camel tome). However, instead of starting with what I don't understand, let me start with what I do (I do understand your first reply to this post and even much of the Linux article referenced).

    It appears that in the first line:
    my %hash = map { my $item = pop @$_; map { $_, $item } @$_ }
    you are mapping a key and value to %hash from an array. Next, it looks like a string of "HELP" redirects flow to a sub (as does the last line "QUIT"...).

    Now, what I don't understand, specifically, how are the keys and values being mapped into the hash (where are they coming from)? What does pop @$_ do (couldn't find @$_ explained anywhere)? How does the last bit on that first line work? map { $_, $item } @$_

    Lastly, why are there square brackets bookending lines 2 and 3?

    I may not be ready for this heady stuff, but I'm willing to try. Thanks merlyn.

    Update:
    Thanks to runrig and his reply. I get it in theory, but not in practice. Though it has encouraged a more thorough understanding of map. I'll look at this in a couple of months and probably get it fine. Thanks.

    —Brad
    "A little yeast leavens the whole dough."
      'Bookending' things with square brackets creates an array reference (see perldoc perlref). So in the outer map, for each bookended thing, $_ is set to that array reference. @$_ dereferences that array reference, so my $item = pop @$_ removes the last item from the array (which is the subroutine reference), and puts it in $item. Then in the inner map we loop over everything left in the array (all the multiple keys) and create key/value hash pairs out of our multiple keys where the value is the subroutine reference that we popped off in the first place.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found