find words not in an array

R

r

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
 
S

ssmith

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";}
 
J

Jürgen Exner

r said:
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.

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
 
M

mbstevens

#!/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
}
 
J

Joe Smith

mbstevens said:
# 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top