The easiest to handle the headers is to read from the file once before going into the while loop. Also, check the return status of your open statements. You might try something like this (untested, and not sure this is really what you want):
# ... open my $in1, "<", $file1 or die "unable to open $file1 $!"; open my $in2, "<", $file2 or die "unable to open $file2 $!"; open my $out, '>', $file3 or die "unable to open $file3 $!"; my $header1 = <$in1>; my $header2 = <$in2>; print $out "some header for output file \n"; # now you are all set with headers, you can focus on the data while (my $line1 = <in1>) { my $line2 = <in2>; last unless defined $line2; chomp $line1, $line2; my @values1 = split $line1; my @values2 = split $line2; my @values3 = map {$values1[$_] - $values2[$_]} 0..$#values1; print $out "@values3 \n"; }
More code is needed for error handling (what is one line has more items than the other? etc.), but I guess you get the basic idea.
Update: removed sentence about return status of return statement, I had not paid attention to the use autodie statement. Also corrected a couple of typos in the code.
In reply to Re: Its a pretty simple question.. though embarassed to ask but i am totally new in perl
by Laurent_R
in thread Its a pretty simple question.. though embarassed to ask but i am totally new in perl
by gb92
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |