Solved

Adding values stored in two file

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expect & sshpass package for Solaris 10 x86 15 990
rename outfile before writing 2 76
iSeries PERL Scripts 7 183
what are these modules in perl script 4 104
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

679 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