Permalink
Browse files

Picking part of hash

  • Loading branch information...
1 parent ccebf36 commit f7b07676fa540395cb94f86b15c8a5f1ba62bc40 @bvr committed Apr 5, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 scripts/2012-04-03-pick.pl
View
@@ -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.