If I do the following, the $c
becomes 1
where I was hoping it would be 2
, as a compact way to count the number of grep
matches.
my %h = (
"abc" => undef,
"abcd" => undef,
"abcde" => undef
);
my $c = 0;
$c++ if grep {/bcd/} keys %h;
print $c;
What would be the correct way to count the number of grep
matches in this case?
Just assign counter to grep,
my $c = grep {/bcd/} keys %h;
From perldoc
In scalar context, returns the number of times the expression was true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With