Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Selecting particular key-value pairs in hashes

by pbeckingham (Parson)
on Sep 18, 2004 at 17:05 UTC ( #392006=note: print w/ replies, xml ) Need Help??


in reply to Selecting particular key-value pairs in hashes

Non-destructively:

my %hash2 = map {$_ => $hash{$_}} grep {/^\d+_\d+$/} keys %hash;



pbeckingham - typist, perishable vertebrate.


Comment on Re: Selecting particular key-value pairs in hashes
Download Code
Re^2: Selecting particular key-value pairs in hashes
by Aristotle (Chancellor) on Sep 18, 2004 at 20:34 UTC

    You can pick more than one value at a time out of a hash — see merlyn's code and gaal's remark.

    my %hash2 = @hash{ grep /\d+_\d+/, keys %hash };

    Update: thanks to bobf for notifying me that this code makes, uh, very little sense. Here's a hash slice incantation that actually works:

    my %hash2; @hash2{ @$_ } = @hash{ @$_ } for [ grep /\d+_\d+/, keys %hash ]; # or more verbosely my %hash2; { my @key = grep /\d+_\d+/, keys %hash; @hash2{ @key } = @hash{ @key }; }

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-07-11 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (222 votes), past polls