Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How can you check if a word in a array occur in another array?

by PerlVader (Initiate)
on Nov 29, 2012 at 08:02 UTC ( #1006184=perlquestion: print w/ replies, xml ) Need Help??
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 tree

woordelysEng.txt- the cat dog

It does not print anything to the output file.

Comment on How can you check if a word in a array occur in another array?
Download Code
Re: How can you check if a word in a array occur in another array?
by frozenwithjoy (Curate) on Nov 29, 2012 at 08:29 UTC
    To avoid complicated nested loops, I'd make use of a hash to index your first file and then look up the second list of words in the hash. Here is something a bit more simple that you can mess with to fit your needs:
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $file_1 = "the cat ran up the tree the cat the dog then barked at the cat the +cat jumped out of the tree"; my $file_2 = "the cat dog"; my %words; $words{$_}++ for split / /, $file_1; say "$_: $words{$_} occurrence(s)" for split / /, $file_2; __END__ the: 7 occurrence(s) cat: 4 occurrence(s) dog: 1 occurrence(s)
Re: How can you check if a word in a array occur in another array?
by 2teez (Priest) on Nov 29, 2012 at 08:47 UTC

    Hi PerlVader,

    Since you have similar codes only to open, read and split into "words" for both of the files you are using. Why not, put common codes into a subroutine, then pass each of the file into it.
    Then using a for/foreach loop, check the arrays. like so:

    #!/usr/bin/perl use warnings; use strict; my $first_array = get_words('short.txt'); my $second_array = get_words('woordelysEng.txt'); foreach my $value ( @{$first_array} ) { foreach ( @{$second_array} ) { print $_, " occurs in first text file\n" if $value eq $_; } } sub get_words { my $filename = shift; my @generated_word; open my $fh, '<', $filename or die "can't open file: $!"; while (<$fh>) { push @generated_word, split; } return \@generated_word; }
    Output:
    the occurs in first text file cat occurs in first text file the occurs in first text file the occurs in first text file cat occurs in first text file the occurs in first text file dog occurs in first text file the occurs in first text file cat occurs in first text file the occurs in first text file cat occurs in first text file the occurs in first text file

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: How can you check if a word in a array occur in another array?
by tobyink (Abbot) on Nov 29, 2012 at 08:49 UTC

    This line:

    @words = split(/ /, $word);

    ... replaces the @words array each time around the loop. So when you're finished looping, @words will only contain words from the last line of the file, words from the previous lines having been discarded.

    What you want is:

    push @words, split(/ /, $word);

    I leave @EN = split(/ /, $word) as an exercise for the reader.

    Also, I concur with frozenwithjoy in that a hash is more likely to suit your needs than an array.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
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
      your code is write just remove the last code part::::::what are u doing here in last part. take a look : #!/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"; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006184]
Approved by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-07-28 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls