]> git.donarmstrong.com Git - debbugs.git/commitdiff
add hash slice
authorDon Armstrong <don@donarmstrong.com>
Thu, 29 Jul 2010 21:49:00 +0000 (17:49 -0400)
committerDon Armstrong <don@donarmstrong.com>
Thu, 29 Jul 2010 21:49:00 +0000 (17:49 -0400)
Debbugs/Common.pm

index ef1b8bbe96cb75e746ed00be5fd52b0b620a97f1..ab51dbacf5c86f5ab20ce9da4ff22f96a150f4b5 100644 (file)
@@ -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;