> 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?
> Thanks!


Something like this should also work.

#!/usr/bin/perl

use strict;
use warnings;

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


foreach my $x (@keywords) {
    foreach my $i (@list1) {
        if ($i eq $x) {
            print "$i is in both arrays\n";
        }
    }
    
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to