Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

RE: multiple keys - one value

by merlyn (Sage)
on Sep 27, 2000 at 23:34 UTC ( #34267=note: print w/replies, xml ) Need Help??


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

my %hash = map { my $item = pop @$_; map { $_, $item } @$_ } [qw(HELP ?) => sub { ... }], [qw(QUIT EXIT LEAVE) => sub { ... }], ...;

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: RE: multiple keys - one value
by japhy (Canon) on Sep 28, 2000 at 01:55 UTC
    Thank you, Randal. That looks about right, and perfectly Perlish.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
Re: RE: multiple keys - one value
by bradcathey (Prior) on Dec 04, 2003 at 14:20 UTC
    I know I'm only 3 years late, but could someone explain/dissect in teaching terms, just what merlyn did. /msg me if you wouldn't mind so I know it's up. Thanks.

    —Brad
    "A little yeast leavens the whole dough."
        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."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://34267]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-07-17 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (381 votes). Check out past polls.

    Notices?