Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Hash assignments using map

by japhy (Canon)
on Feb 25, 2007 at 00:15 UTC ( #601938=note: print w/ replies, xml ) Need Help??


in reply to Hash assignments using map

In addition to what others have said, you could do:

my @to_keep = (...); my %keepers; @keepers{@to_keep} = ();
which you would then test with exists $keepers{$x}. Althought I'm curious why you need the %keepers hash if you have the @to_keep array.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart


Comment on Re: Hash assignments using map
Select or Download Code
Re^2: Hash assignments using map
by njcodewarrior (Pilgrim) on Feb 25, 2007 at 02:52 UTC

    Hi japhy

    Not to beat a dead horse here, but to answer your question:

    I'm checking the keys in another hash against those in %keepers. If a keys exists in this other hash, but NOT in %keepers, I want to eliminate it from the hash. See this node for further explanation.

    Anyways, thanks for the clarification

    njcodewarrior

      Right. My point is, why not loop over the elements in @keepers, rather than the keys of the other hash?
      my %big_hash = (...); my @to_keep = ('abc', 'xyz'); %big_hash = map { exists($big_hash{$_}) ? ($_ => $big_hash{$_}) : () } + @to_keep; # or %big_hash = map { $_ => $big_hash{$_} } grep { exists $big_hash{$_} } +@to_keep;

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2015-07-06 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls