i’m working on a simple loop but it’s not working. I have 2 files which i compare based on a common id. It sort of works because it outputs the first result and the first result only! so it’s not looping through the rest of the files (or last file because the output contains all the lines from the first file). It outputs all the lines from the first file but it only appends the first result from the second file into the outfile. Here is my code to clarify:
use strict;
use warnings;
my $inputfile1 = shift || die "Give input & output!\n";
my $inputfile2 = shift || die "Give input & output!\n";
my $outputfile = shift || die "Give input!\n";
open my $INFILE1, '<', $inputfile1 or die "In use / Not found :$!\n";
open my $INFILE2, '<', $inputfile2 or die "In use / Not found :$!\n";
open my $OUTFILE, '>', $outputfile or die "In use / Not found :$!\n";
while (<$INFILE1>) {
s/"//g;
my @elements = split /;/, $_;
while (<$INFILE2>) {
s/"//g;
my @loopelements = split /;/, $_;
if ($elements[11] eq $loopelements[0]){
$elements[12] = $loopelements[1];
$elements[13] = $loopelements[2];
}
}
my $output_line = join(";", @elements);
print $OUTFILE $output_line;
#print "\n"
}
close $INFILE1;
close $INFILE2;
close $OUTFILE;
exit 0;
My first attempt was this piece of code, and it worked partially. Why?: halfway through it crashes. When i check the outputfile it worked to about halfway and stops. No idea why! And on a side note, i think the one below is less efficiënt or is there a better alternative for both?
$inputfile1=$ARGV[0];
$inputfile2=$ARGV[1];
$outputfile1=$ARGV[2];
open(INFILE1,$inputfile1) || die "Give input & output :$!\n";
open(INFILE2,$inputfile2) || die "Give input & output :$!\n";
open(OUTFILE_1,">$outputfile1") || die "Give input & output :$!\n";
$i = 0;
$j = 0;
@infile1=<INFILE1>;
@infile2=<INFILE2>;
foreach ( @infile1 )
{
@elements = split(";",$infile1[$i]);
$j=0;
foreach ( @infile2 )
{
@loopelements = split(";",$infile2[$j]);
if ($elements[11] eq $loopelements[0]){
$elements[12] = $loopelements[1];
$elements[13] = $loopelements[2];
$printen = 1;
last;
}
$j = $j+1;
}
@elements = join(";",@elements);
print "$i\r";
if ($printen == 1) { print OUTFILE_1 "@elements"; };
$i = $i+1;
}
close(INFILE1);
close(INFILE2);
close(OUTFILE_1);
So can someone point out where i’m going wrong in my code at the top?
The first line of first file is read in the first iteration of outer loop.
During this first iteration itself, all the lines of second file are read in the inner loop.
Then the first iteration of outer loop ends.
Now, second iteration of outer loop comes up. Are there any lines of second file remaining to read anymore? No.
Breaking the problem down to its simplest, the two lines with comments next to them make the program loop through the lines of the second file each time:
If this is too slow, I see two things you can do:
What I mean by this is: