473,549 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10114
Not very elegant, but try this...

my @wordlist = qw / one two three four seven / ;
my @testlist = qw / two four five six / ;
my @testminuswordl ist;

foreach $testitem (@testlist) {
$found = 0;
foreach $worditem (@wordlist) {
if ($testitem eq $worditem) {
$found=1;
last;
}
}
if (!$found) {push @testminuswordl ist, $testitem};

}

foreach (@testminusword list) {print $_ . "\t";}
"r" <jk!ttop5@mnpX$ .net> wrote in message
news:ar******** ************@co mcast.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1819
by: bruno storz | last post by:
Hit here, I am looking for an example how to select a or several files. I have a directory with a bunch of files. Now I want to use a form which allows me to enter some words which will be used to select files containing those words. Do you have any idea?
1
1663
by: t0M | last post by:
It's nearly impossible to find anything on this because of the Dictionary class, included within the dotnet framework, that pollutes any search results pertinent to my question. I want to be able to access an array of words, just like it were a physical dictionary. It's for a stupid little project, but it involves decryption. Is there a...
13
12296
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist. Thanks ever so much in advance
16
3066
by: Java script Dude | last post by:
Creating a method of object (Object.prototype.classOf ...) is not the correct way because of a (as expected) flaw in IE where DOM Elements does not inherit from Object. As a result it is best to create a stand alone method passing the object as an argument: ~~~~ CODE ~~~~ <div id=divTest></div>
7
3180
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends up looking for characters that wrap around the stripped characters and it ends up as a recursive ordeal that fails to identify a poorly constructed...
20
5105
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want to do it word by word. I am confused as to how to do this. Typically, I would think it would make sense to try and input the words into strings, but...
1
4203
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
2
2523
by: JasonC | last post by:
Hi, First time posting here so please be gentle! I wish to check a variable for a number of words that are in a array. Not sure if this is the best way to do it, but this is what i have. I have my own counter system that counts every visitor, but some of these visits are from BOTS, spiders..
6
1541
by: Bint | last post by:
I have an array whose elements I'm accessing, like array, array, etc. However, the data is meant to be 16-bit words, not bytes. I'm getting byte values right now. Is there any way I can tell php that an array is composed of words and not bytes? Thanks B
18
3851
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a given file. I tried my level best even after the quiz to come up with a solution but cudnt find an efficient one. :( This is what I did.
0
7550
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7836
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5116
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3502
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
789
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.