I thought I might use grep but can't figure it out.
Thanks,
r
my @wordlist = qw / one two three four seven / ;
my @testlist = qw / two four five six / ;
my @testminuswordlist;
foreach $testitem (@testlist) {
$found = 0;
foreach $worditem (@wordlist) {
if ($testitem eq $worditem) {
$found=1;
last;
}
}
if (!$found) {push @testminuswordlist, $testitem};
}
foreach (@testminuswordlist) {print $_ . "\t";}
"r" <jk!ttop5@mnpX$.net> wrote in message
news:arqdnSYdyYA...@comcast.com...
No, grep isn't quite the right tool. For questions like that a hash is
usually the datastructure of choice.
In this particular case you may want to start with the "perldoc -q
intersection". This FAQ computes the symmetric difference, so you will have
to modify the answer slightly.
Or you simply grab the proper set module from CPAN.
jue
foreach my $n (@newlist)
{print "$n\n";}
#-------------------------------------------
sub
is_in_wordlist {
my $sought = shift;
foreach my $w (@wordlist) {
if ($sought eq $w)
{ return 1; } # found
}
return 0; # not found
}
Your solution does not scale well. If @wordlist and @testlist
have 1000 words each, the brute-force method requires 1000000
string comparisons instead of just 2000 hash operations.
The answer found in the FAQ is better.
-Joe