I was looking for a way to do the following using the perl map
function: I want to extract pairs (key, value) in which the value is equal to or matches the specified parameter.
In my example, I want to extract pairs (key, value), where value = failed
, but can also be an expression (i.e. a line starting with A or REGEX). That's why I want the resulting hash, and not just a table of 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 to do this, but without great results, so I think I'm confused about links, affects, 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);
In this case, it would be impossible or inefficient to use the card, but it would help me (and possibly others) find out why.
source share