Solved

Adding values stored in two file

Posted on 2004-08-05
5
187 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

23 Experts available now in Live!

Get 1:1 Help Now