Solved

Adding values stored in two file

Posted on 2004-08-05
5
204 Views
Last Modified: 2010-03-05
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";

}
}

0
Comment
Question by:indie_campbell
  • 2
5 Comments
 

Author Comment

by:indie_campbell
ID: 11728021
sorry forgot to omit $y, $z, $y1 and $z1
0
 
LVL 11

Accepted Solution

by:
bcladd earned 50 total points
ID: 11730210
Perhaps this makes more sense:

open (FILE1, "<$file1");
open (FILE2, "<$file2");
open (OUT, ">$output");

while (($num1 = <FILE1>) &&
       ($num2 = <FILE2>)) {
    print OUT $num1 + $num2;
    print OUT "\n";
}

close OUT;
close FILE2;
close FILE1;

-bcl
0
 
LVL 11

Expert Comment

by:bcladd
ID: 11730232
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now