I have been looking for a way to do the following action using the perl function map:
given a hash, I want to extract the pairs (key, value) in which the value equals or matches a specified parameter.
In my example I want to extract the (key, value) pairs where value = failed, but it could also be an expression (i.e. string starting with A, or a REGEX). That’s why I want as a result a hash and not only a table of the keys matching the value.
my %strings = (
bla => "success",
ble => "failed",
bli => "failed",
foo => "success",
blo => "failed",
bar => "failed",
blu => "success"
);
my %failed_s = ();
while (my ($k, $v) = each %strings) {
if ( $v eq 'failed' ) {$failed_s{$k} = $v};
};
I tried several ways of doing this, but without great results so I think I’m getting confused about references, affectation, results, etc.
my %failed_s =
map { { $_ => $strings{$_} }
if ( $strings{$_}./failed/ ) }
keys %strings;
my %failed_s =
map { ( $strings{$_} eq 'failed') &&
($_, $strings{$_}) }
keys %strings;
print "Dumper \%failed_s: \n" . Dumper(\%failed_s);
It might be not possible or not effective to use map, in this case, but it would help me (and probably others) to know why.
In your first map example, you are returning single element hash references, which is not what you want.
In your second, the
&&operator imposes scalar context on its arguments, so the list($_, $strings{$_})only returns the last item in the list.What you are looking for is:
Where the
? :operator returns the list if the condition is true, and an empty list if it is false.