Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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


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

pbeckingham - typist, perishable vertebrate.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://392006]
[ambrus]: GotToBTru: wait, you tell only the atom totals of what you want? Is that like ordering food in a restaurant by telling only the nutrient amounts you need, or
[ambrus]: like when a medieval scientist supposedly proves his priority inventing something by having previously published an anagram of a thousand letters long summary of the invention?
[choroba]: Progress! Nowadays, you can write a Perl script to generate such an anagram!
[GotToBTru]: i like to order my chemicals a-la-carte ambrus ;)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2016-12-06 13:07 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (104 votes). Check out past polls.