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.