user2256825
user2256825

Reputation: 604

grep multiple pattern in perl array at a time

Below is the code which actually finds a pattern in a perl array.

 my $isAvailable = grep { $_->[0] eq '12345' } {$filteredTableEntriesMap{$REPORT_PART1}} ;

But i would like to search for two patterns in two indexes at a time

 my $isWiuAvailable = grep { $_->[0] eq '12345' }     @{$filteredTableEntriesMap{$REPORT_PART1}} ;
 my $isBsAvailable  = grep { $_->[1] eq '6789' } @{$filteredTableEntriesMap{$REPORT_PART1}} ;

This is how the map is represented

 $VAR1 = {
      'REPORT PART2' => [],
      'REPORT PART1' => [
                               [
                                 '12345',
                                 '6789',                         
                               ],
                               [
                                 '343435',
                                 '315',
                               ],
                               [
                                 '00103',
                                 '000315',

                               ],
                        ]   

And i would want to match an array which has these two entries in index 1 and index 2

Thanks

Upvotes: 0

Views: 2804

Answers (1)

simbabque
simbabque

Reputation: 54381

You can combine the two conditions into one expression.

my @found = grep { $_->[0] eq '12345' && $_->[1] eq '6789' }
   @{$filteredTableEntriesMap{$REPORT_PART1}};

The stuff inside the {} for grep is basically a subroutine. You can do as much as you want in there as long as you return a true value if you want to keep $_ in your @found result.

Upvotes: 2

Related Questions