Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Laurent_R (Prior)
on Aug 13, 2013 at 21:42 UTC ( #1049347=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

OK, following your messages on the chatterbox (I was not really on-line, I just had my browser still open on Perlmonks, but was not in front of my computer), I've just prepared a new version of the code that I supplied earlier.

I can't say if it does what you want, because I am still not even sure that I understood what you really want and because I don't have actual sample files on which to run tests, but at least, I tested that it should compile cleanly without any error or warning:

use strict; use warnings; my $file1=$ARGV[0]; my $file2=$ARGV[1]; my $file3=$ARGV[2]; 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"; }

This what it should do:

  • read the first line of file1 and file 2 (the headers) and discard them;
  • write a silly header in file3 (you can just change it in accordance with your needs);
  • read into $line1 and $line2 the next line from file1 and file2 respectively;
  • split $line1 and $line2 into the @values1 and @values2 arrays;
  • substract respective values of @values2 from @values1;
  • output the restults of these substractions to the output file;
  • repeat the last four steps for the next line, until the files are exhausted.

So, if file1 has:

foo bar baz 43 41 67

And file2 has:

foo bar baz 41 2 60

The output would be:

Some new header to be determined 2 39 7

That is at least what I understood from your initial description of your requirement.

As I already said, this program would need additional error-handling code (if the files don't match), but I am not going to go into that so long as I don't even know if this is what you need.


Comment on Re: Its a pretty simple question.. though embarassed to ask but i am totally new in perl
Select or Download Code
Re^2: Its a pretty simple question.. though embarassed to ask but i am totally new in perl
by gb92 (Initiate) on Aug 14, 2013 at 04:46 UTC

    thank you so much laurent sir ! understood it quite well really thanks a lot sir :) and yes this was what i needed

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2015-07-02 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls