Home > Archive > PERL Beginners > May 2007 > RegEx again
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Yitzle 2007-05-20, 3:58 am |
| 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!
| |
| John W. Krahn 2007-05-20, 3:58 am |
| yitzle wrote:
> 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.
perldoc -q "How do I compute the intersection of two arrays"
John
| |
| Ken Foskey 2007-05-20, 3:58 am |
| On Sun, 2007-05-20 at 01:42 -0400, yitzle wrote:
> # 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);
Problem is you need slashes.
print "2" if (grep /$searchRegEx/, @list2);
I also wonder why you did not use the simpler:
$searchRegEx = '^('. join( '|',@keywords ). ')$';
--
Ken Foskey
FOSS developer
| |
| Dr.Ruud 2007-05-20, 7:58 am |
| yitzle schreef:
> Subject: RegEx again.
Please put your question in the Subject.
Your question is more like:
how to compare two arrays
(And it doesn't even have to use a regular expression, right?)
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| yaron@kahanovitch.com 2007-05-20, 7:58 am |
| Hi,
Assuming @keywords and @list1 and @list2 you can use Array::Diff (http://se=
arch.cpan.org/~typester/Array-Diff-0.04/lib/Array/Diff.pm)
use Array::Diff;
@list1 =3D qw/big bad blue ball/;
@list2 =3D qw/box sand house/;
@keywords =3D qw/brown black blue/;
@list1 =3D sort(@list1);
@list2=3Dsort(@list2)
@keywords=3Dsort(@keywords);
<code>
my $diff =3D Array::Diff->diff( \@keywords, \@list1 );
print "1\n" if $diff->count;
$diff =3D Array::Diff->diff( \@keywords, \@list2 );
print "2\n" if $diff->count;
</code>
This might give you a hint.
Yaron Kahanovitch
----- Original Message -----
From: "yitzle" <yitzle@users.sourceforge.net>
To: "Perl Beginners" <beginners@perl.org>
Sent: 22:42:29 (GMT-0800) America/Tijuana =D7=A9=D7=91=D7=AA 19 =D7=9E=D7=
=90=D7=99 2007
Subject: RegEx again
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 =3D qw/big bad blue ball/;
@list2 =3D qw/box sand house/;
@keywords =3D qw/brown black blue/;
# Add a ^ and $ so the strings match fully
push @search, qr/^$_$/ for ( @keywords );
$searchRegEx =3D join '|',@search;
print "1" if ( grep $searchRegEx, @list1 );
print "2" if (grep $searchRegEx, @list2);
Result: "12"
Um. Please help?
Thanks!
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Romeo Theriault 2007-05-20, 6:59 pm |
|
> 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";
}
}
}
|
|
|
|
|