Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Read and analyze two file in parallel

by remluvr (Sexton)
on Mar 14, 2012 at 22:06 UTC ( [id://959676]=note: print w/replies, xml ) Need Help??


in reply to Re: Read and analyze two file in parallel
in thread Read and analyze two file in parallel

Thanks! In the meanwhile I tried something and it worked, I post it here in case it could be helpful to someone (or in case you have suggestions to give me on it). Here it is:

sub read_file_line { my $fh = shift; if ($fh and my $line = <$fh>) { chomp $line; return [ split(/\t/, $line) ]; } return; } #sub compute { # do something with the 2 values #} open(my $f1, $input); open(my $f2, $input_1); my $pair1 = read_file_line($f1); my $pair2 = read_file_line($f2); my $value; open OUT,">$file"; while ($pair1 and $pair2) { $value=$pair1->[3]*(1-$pair2->[3]); $pair1 = read_file_line($f1); $pair2 = read_file_line($f2); print OUT $pair1->[0]."\t".$pair1->[1]."\t".$pair1->[2]."\t".$valu +e."\n"; } close($f1); close($f2); close OUT;

Thanks,
Giulia

Replies are listed 'Best First'.
Re^3: Read and analyze two file in parallel
by aaron_baugher (Curate) on Mar 14, 2012 at 22:21 UTC

    Some suggestions:
    That's a bit awkward, because you call read_file_line() inside and outside your while loop, so maintenance-wise it would be easy to change one and forget to change the other. You could combine both calls into your while loop condition:

    while(my $pair1 = read_file_line($f1) and my $pair2 = read_file_line($ +f2)){

    Also, since you only use $value inside the loop, there's no need to declare it outside. Just declare it with my when you assign to it inside the loop. Declaring it outside the loop means it'll retain its value between loops, which shouldn't hurt in this case, but can make odd things happen if the assignment to it is ever conditional.

    In your print statement, there's no need to concatenate all those things. Just put your variables and \t characters inside one set of quotes.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found