Solved

Adding values stored in two file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

12 Experts available now in Live!

Get 1:1 Help Now