Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Its a pretty simple question.. though embarassed to ask but i am totally new in perl

by Laurent_R (Parson)
on Aug 13, 2013 at 14:16 UTC ( #1049283=note: print w/ replies, xml ) Need Help??


in reply to Its a pretty simple question.. though embarassed to ask but i am totally new in perl

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.


Comment on Re: Its a pretty simple question.. though embarassed to ask but i am totally new in perl
Download Code
Re^2: Its a pretty simple question.. though embarassed to ask but i am totally new in perl (autodie)
by Anonymous Monk on Aug 13, 2013 at 14:34 UTC

    Also, check the return status of your open statements

    That is what autodie does for the OP

      Right, I did not notice the use autodie statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049283]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-12-20 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls