Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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 ( #1006198=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re: How can you check if a word in a array occur in another array?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006198]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-23 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (216 votes), past polls