loops - perl read same line from two files -


i have 2 files same number of lines, each containing columns of numeric values.

example of file a

1 2 3 4 2 3 4 5 

example of file b

7 8 9 0 6 7 8 9 

i want sum value of corresponding lines both of these files, , write results output file.

the expected output:

8 10 12 4 8 10 12 14 

considering 2 files has same number of lines , fields

use strict; use warnings; use data::dumper;  #first data file open $fh1, '<', '1.txt' or die $!;  #seecond data file open $fh2, '<', '2.txt' or die $!;  #output file open $out, '>', 'out.txt' or die $!;  while (!eof($fh1) , !eof($fh2)) {     $line1 = <$fh1>;     $line2 = <$fh2>;      @l1 = split /\s+/, $line1;     @l2 = split /\s+/, $line2;      @newvalues;      $i = 0;     (@l1){         push @newvalues, $_ + $l2[$i];         $i++;     }      print dumper \@newvalues;      $new = join ' ', @newvalues;     print $out $new."\n"; } 

## edit ##

see @hwnd version below cleaner , more compact code on how solve problem


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -