Help loop confusion

O

omurice

Hi,..

I meant to compare lines like this in two files

$set1;
$set2;
$varline1;
$varline2;
$i = 0;
$j = 0;

foreach my $line (@file2Arr) {
$line2 = @file2Arr;
foreach my $line (@file1Arr){
$line1 = @file1Arr[j];
if (line1 is similar to line2){
$set1 = $j + 1; #file1 line number
$set2 = $i + 1; #file2 line number
...
do things;
...
$varline1 = $set1;
$varline2 = $set2;
last;
}
else{
$j++;
}
}#end of foreach j
$i++;
}#end of foreach i

I can get out of foreach j loop when match is found and proceed to the
next i,..but how do I get the j loop to start again from the index
where match is last found + 1
So for example, if match is found on file1 line 4 and file2 line 1
($i=0, $j=3), I'd like to start the next looping with $i = 1, and $j =
4

Help me pleeeeaseeeee!!!!!!!
Thaaaaanksssss
 
B

Brian McCauley

omurice said:
$set1;
$set2;
$varline1;
$varline2;

What are the purpose of those lines? Are they perhaps supposed to be
declarations but you forgot the 'my's?

If you'd put 'use strict' and 'use warnings' perl would have caught that
mistake for you.
$i = 0;
$j = 0;

foreach my $line (@file2Arr) {
$line2 = @file2Arr;


There are two mistakes in that line. If you'd put 'use strict' and 'use
warnings' perl would have caught those mistakes for you.

Anyhow what's the point of $line2? You already have the elements of
@file2Arr in $line.
foreach my $line (@file1Arr){
$line1 = @file1Arr[j];
Ditto.

if (line1 is similar to line2){
$set1 = $j + 1; #file1 line number
$set2 = $i + 1; #file2 line number
...
do things;
...
$varline1 = $set1;
$varline2 = $set2;
last;
}
else{
$j++;
}
}#end of foreach j
$i++;
}#end of foreach i

There are no foreach i or j loops in your code.

Please try to illustrate your point by constructing a minimal but
complete strict warning free script.
I can get out of foreach j loop when match is found and proceed to the
next i,..but how do I get the j loop to start again from the index
where match is last found + 1
So for example, if match is found on file1 line 4 and file2 line 1
($i=0, $j=3), I'd like to start the next looping with $i = 1, and $j =
4

So whynot just do so?
Help me pleeeeaseeeee!!!!!!!

Ask machines for help first pleeeeaseeeee!!!!!!!

OK let's take a step back and find out what you want to achieve.

I'm guessing you want to scan @file2Arr for strings that also appear in
@file1Arr but once you've found one you then want to stop looking for
elements that appeared earlier in @file1Arr.

use strict;
use warnings;

my $file1Offset = 0;

# I assume in reality is_similar() is more complex
# If it were simple equality there would be much simpler ways
sub is_similar { $_[0] eq $_[1] }

my @file1Arr = qw( sat cat the );
my @file2Arr = qw( The cat sat on the mat );

for my $i (0 .. $#file2Arr) {
# I'm assuming you really need $i - otherwise just iterate over
# @file2Arr directly
my $line2 = $file2Arr[$i];
for my $j ( $file1Offset .. $#file1Arr ) {
my $line1 = $file1Arr[$j];
if (is_similar($line1,$line2)) {
# do things;
print "$line2\n";
$file1Offset = $j+1;
last;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,934
Members
47,472
Latest member
KarissaBor

Latest Threads

Top