PerlVader has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have 2 short arrays with only words in them. I want to check if a word in the first array occurs in the second array, but I am having some problems.
#!/usr/bin/perl -w use strict; use warnings; use utf8; open (INPUT, "<:utf8", "short.txt") or die "can't open"; open (INPUT2, "<:utf8", "woordelysEng.txt") or die "can't open"; open (OUTPUT, ">output.txt") or die "can't open"; my @words; my @EN; while (<INPUT>) { my $word = $_; chomp $word; @words = split(/ /, $word); } while (<INPUT2>) { my $word = $_; chomp $word; @EN = split(/ /, $word); } for(my $z = 0; $z <= $#EN; $z++) { for(my $y = 0; $y <= $#words; $y++) { if($EN[$z] eq $words[$y]) { print OUTPUT "$EN[$z]\n"; } } } for(my $z = 0; $z <= $#EN; $z++) { foreach my $correct(@words) { if ($EN[$z] eq "$correct") { print "$EN[$z]\n"; } } }
I tried 2 different ways of looping through it but without success. Any advice? Here is the 2 text files:
short.txt- the cat ran up the tree the cat the dog then barked at the cat
the cat jumped out of the treewoordelysEng.txt- the cat dog
It does not print anything to the output file.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How can you check if a word in a array occur in another array?
by tobyink (Canon) on Nov 29, 2012 at 08:49 UTC | |
|
Re: How can you check if a word in a array occur in another array?
by frozenwithjoy (Priest) on Nov 29, 2012 at 08:29 UTC | |
|
Re: How can you check if a word in a array occur in another array?
by 2teez (Vicar) on Nov 29, 2012 at 08:47 UTC | |
|
Re: How can you check if a word in a array occur in another array?
by Anonymous Monk on Nov 29, 2012 at 09:06 UTC | |
by ashish20092009 (Novice) on Nov 29, 2012 at 11:54 UTC |