indie_campbell
asked on
Adding values stored in two file
Hi all,
I am unable to get this code working correctly, i need to sum the following:
F1:
1000
50
F2
1000
50
Output:
2000
100
so summing the identical line of the 2 files together. Here is my code, probbaly not the best of ideas?
open (FILE, "<$file");
while (<FILE>) {
#@array = ( );
chomp;
( $x, $y, $z) = split (/\s+/, $_);
open (FILE2, "<$file2");
while (<FILE2>) {
chomp;
($x1, $y1, $z1) = split (/\s+/, $_);
$sum = $x+$x1;
push @array, $sum;
open (OUT, ">output");
print OUT "@array\n";
}
}
I am unable to get this code working correctly, i need to sum the following:
F1:
1000
50
F2
1000
50
Output:
2000
100
so summing the identical line of the 2 files together. Here is my code, probbaly not the best of ideas?
open (FILE, "<$file");
while (<FILE>) {
#@array = ( );
chomp;
( $x, $y, $z) = split (/\s+/, $_);
open (FILE2, "<$file2");
while (<FILE2>) {
chomp;
($x1, $y1, $z1) = split (/\s+/, $_);
$sum = $x+$x1;
push @array, $sum;
open (OUT, ">output");
print OUT "@array\n";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Note that OUT will only have as many lines as there are in the shorter input file.
Also assumes that the lines are numeric (no checking is done).
This version doesn't read everything into memory or generate the output in an array; it might be better to "write" it into a buffer and then only print it to the file if all goes well (no non-numeric lines, same number of lines in both files).
-bcl
Also assumes that the lines are numeric (no checking is done).
This version doesn't read everything into memory or generate the output in an array; it might be better to "write" it into a buffer and then only print it to the file if all goes well (no non-numeric lines, same number of lines in both files).
-bcl
ASKER