in reply to Re: Simplest way to match/filter 2d array of values to search in perl
in thread Simplest way to match/filter 2d array of values to search in perl
Straightforward solution:
my $matches = 2; my $threshold = 8; my @results = (); foreach my $row (@arr) { my $count; for (1..$#$row) { $count++ if($row->[$_] >= $threshold); } push @results, $row if($count >= $matches); } foreach(@results) { say join ",", @$_; }
Shorter/idiomatic/obfuscated (take your pick) solution:
my $matches = 2; my $threshold = 8; my @results = grep { $matches <= scalar grep { $_ >= $threshold } @$_[1..$#$_]; } @arr; foreach(@results) { say join ",", @$_; }
Both of these output:
D,6,7,8,8 F,4,8,8,8
BTW, I'm assuming here that you're actually interested in whether there's at least $matches item equal to or greater than $threshold, since that's what "threshold" implies. If you're only interest in exactly that value, change >= $threshold to == $threshold in either code snippet above.
|
|---|