Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find words not in an array

57 views
Skip to first unread message

r

unread,
Mar 27, 2005, 11:09:51 PM3/27/05
to
if I have 2 arrays, @wordlist and @testlist, how can I create a third array
that contains the words from @testlist that are not common to @wordlist?

I thought I might use grep but can't figure it out.
Thanks,
r


ssmith

unread,
Mar 28, 2005, 12:42:55 AM3/28/05
to
Not very elegant, but try this...

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...

Jürgen Exner

unread,
Mar 28, 2005, 1:18:17 AM3/28/05
to

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


mbstevens

unread,
Mar 28, 2005, 2:46:14 AM3/28/05
to
#!/usr/local/bin/perl -w
use strict;
#-----------------------------------------------
# Q: if I have 2 arrays,
# @wordlist and @testlist,
# how can I create a third
# array that contains the words from
# @testlist that are not common to
# @wordlist?
#--------------------------------------------
# A: I like a subroutine version for clarity.
#--------------------------------------------
use subs qw (is_in_wordlist);
my @testlist = ( 'a', 'e', 'i', 'o', 'u');
my @wordlist = ('zot', 'pook', 'e', 'vee', 'u');
my @newlist = ();
#-----------------------------------------
foreach my $t (@testlist) {
if ( !is_in_wordlist($t) )
{push @newlist, $t;}
}

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
}


Joe Smith

unread,
Mar 28, 2005, 2:09:40 PM3/28/05
to
mbstevens wrote:
> # A: I like a subroutine version for clarity.
> foreach my $w (@wordlist) {
> if ($sought eq $w)

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

0 new messages