Sunday, January 2, 2011

How to grep array of arrays

@array1 = ('20020701', 'Sending Mail in Perl', 'Philip Yuson');
@array2 = ('20020601', 'Manipulating Dates in Perl', 'Philip Yuson');
@array3 = ('20020501', 'GUI Application for CVS', 'Philip Yuson');

@main = (\@array1, \@array2, \@array3);
use Data::Dumper ;
#print Dumper \@main ;
#To explain, $_ will contain an array reference. $_->[1] will dereference the array and obtain the element at index 1.
#print grep { $_->[1] =~ /Manipulating Dates in Perl/} @main ;
#print grep { $_->[1] eq 'Manipulating Dates in Perl'} @main ;

@result = grep { "@$_" =~ /Manipulating Dates in Perl/ } @main;

print "@result";

No comments: