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 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
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
#!/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
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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...
|
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
|
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>
|
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...
| |
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...
|
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)
|
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..
|
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
|
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.
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |