From: Don Armstrong Date: Thu, 29 Jul 2010 21:49:00 +0000 (-0400) Subject: add hash slice X-Git-Tag: release/2.6.0~415^2~31 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=891c5cd5a2e1ec1ed95c6d3eff7a0c2595955742;p=debbugs.git add hash slice --- diff --git a/Debbugs/Common.pm b/Debbugs/Common.pm index ef1b8bb..ab51dba 100644 --- a/Debbugs/Common.pm +++ b/Debbugs/Common.pm @@ -48,6 +48,7 @@ BEGIN{ ], misc => [qw(make_list globify_scalar english_join checkpid), qw(cleanup_eval_fail), + qw(hash_slice), ], date => [qw(secs_to_english)], quit => [qw(quit)], @@ -806,6 +807,22 @@ sub cleanup_eval_fail { return $error; } +=head2 hash_slice + + hash_slice(%hash,qw(key1 key2 key3)) + +For each key, returns matching values and keys of the hash if they exist + +=cut + + +# NB: We use prototypes here SPECIFICALLY so that we can be passed a +# hash without uselessly making a reference to first. DO NOT USE +# PROTOTYPES USELESSLY ELSEWHERE. +sub hash_slice(\%@) { + my ($hashref,@keys) = @_; + return map {exists $hashref->{$_}?($_,$hashref->{$_}):()} @keys; +} 1;