ananassa has asked for the wisdom of the Perl Monks concerning the following question:
Good evening/morning everyone! I have a easy code in pearl that look through two arrays and print out the lines that match between the two array. I am using two foreach loops and I am wondering why is my output repeated as many times as the lines of one of my two files. what kind of loop would you suggest? Thanks! Here below is the code
#!/usr/bin/perl -w use strict; open (INA, $ARGV[0])|| die "unable to open file"; open (INB, $ARGV[1])|| die "unable to open file"; my $item; my $line; my @indirizzi = <INB>; my @nomi = <INA>; chomp @nomi; foreach $item(@nomi) { foreach my $line (@indirizzi){ if ($line =~ m/^$item/){ print $line; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Foreach loop explanation
by SuicideJunkie (Vicar) on Nov 23, 2012 at 21:30 UTC | |
|
Re: Foreach loop explanation
by eyepopslikeamosquito (Archbishop) on Nov 24, 2012 at 00:11 UTC | |
|
Re: Foreach loop explanation
by Kenosis (Priest) on Nov 24, 2012 at 01:54 UTC | |
|
Re: Foreach loop explanation
by Don Coyote (Hermit) on Nov 23, 2012 at 21:57 UTC |