469,349 Members | 1,646 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

find words not in an array

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
Jul 19 '05 #1
4 9977
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:ar********************@comcast.com...
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

Jul 19 '05 #2
r wrote:
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
Jul 19 '05 #3
#!/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
}


Jul 19 '05 #4
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bruno storz | last post: by
13 posts views Thread by mike | last post: by
16 posts views Thread by Java script Dude | last post: by
7 posts views Thread by Jim Carlock | last post: by
20 posts views Thread by dmurray14 | last post: by
6 posts views Thread by Bint | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.