?
Solved

Adding values stored in two file

Posted on 2004-08-05
5
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
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…
Six Sigma Control Plans

800 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