in reply to Re: Selecting particular key-value pairs in hashes
in thread Selecting particular key-value pairs in hashes
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.
|
|---|