develooper Front page | perl.beginners | Postings from May 2007

RegEx again

Thread Next
May 19, 2007 22:42
RegEx again
Message ID:
I got two arrays of strings.
I am trying to search to see if any of the strings of one array
matches a string of the other array.

Lists to search:
qw/big bad blue ball/, qw/box sand house/

Search list:
qw/brown black blue/

I should be able to get list #1 but not list #2 because the first list
has "blue" as does the search list.

# Look in
@list1 = qw/big bad blue ball/;
@list2 = qw/box sand house/;
@keywords = qw/brown black blue/;

# Add a ^ and $ so the strings match fully
push @search, qr/^$_$/ for ( @keywords );
$searchRegEx = join '|',@search;
print "1" if ( grep $searchRegEx, @list1 );
print "2" if (grep $searchRegEx, @list2);

Result: "12"

Um. Please help?

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About