user3457622
user3457622

Reputation: 59

Perl: grep a value in array in array

How do I grep values from an array of arrays? My attempt:

my $match =grep (/value/, @array);

if ( $match <= 0 ) { ... }

I am always getting zero as a output which is incorrect.

I am able to print $value after 2 for, loops so I think grep will only work when there is 1 loop.

for $value (array) {
    for my $value1 (@$value) 
    { print $value1 }; 

Thanks.

Upvotes: 0

Views: 1776

Answers (1)

mpapec
mpapec

Reputation: 50637

To grep first level of arrays into @result

my @result = grep { grep { /search/ } @$_ } @array;

grepping final strings into @result,

my @result = grep { /search/ } map { @$_ } @array;

Upvotes: 1

Related Questions