Skip to content

Commit

Permalink
Picking part of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
bvr committed Apr 5, 2012
1 parent ccebf36 commit f7b0767
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions scripts/2012-04-03-pick.pl
@@ -0,0 +1,30 @@

use Data::Dump;

my $hash = {
a => 1,
b => 2,
c => 3,
d => 4,
e => 5,
};

dd $hash;

dd pick($hash,['a'..'c']);

=head2 pick
my $picked_hash = pick \%hash, \@keys;
Pick number of items from hashref. It creates new hashref with items whose keys
are specified in keys arrayref.
=cut
sub pick {
my ($hash, $items) = @_;

my %new_hash;
@new_hash{@$items} = @{$hash}{@$items};
return \%new_hash;
}

0 comments on commit f7b0767

Please sign in to comment.