2

Good morning to everyone, I need some help with this...

I have this peace of code:

open $fh, ">", "./results_BLAST_adjacentgenes_samehit.txt" or die "Could not write the file.";
print "Validating...\n";
$counter = 0;
for $key (keys %hash4000) {
    for $hit1 (@{$hash4000{$key}}) {
        for $hit2 (@{$hash4000{$key}}) {
            $text = $hit1."\t".$hit2."\t".$key."\n";
            next if $text ~~ @array; push @array, $text;
            if ($chromosome{$transc{$hit1}} eq $chromosome{$transc{$hit2}} and $transc{$hit1} ne $transc{$hit2}) {
                @sorted_startshit1 = sort { $a <=> $b } @{$starts{$transc{$hit1}}};
                @sorted_startshit2 = sort { $a <=> $b } @{$starts{$transc{$hit2}}};
                @sorted_endshit1 = sort { $b <=> $a } @{$ends{$transc{$hit1}}};
                @sorted_endshit2 = sort { $b <=> $a } @{$ends{$transc{$hit2}}};
                $start_hit1 = $sorted_startshit1[0];
                $start_hit2 = $sorted_startshit2[0];
                $end_hit1 = $sorted_endshit1[0];
                $end_hit2 = $sorted_endshit2[0];
                $dist1 = $end_hit1 - $start_hit2;
                $dist2 = $start_hit1 - $end_hit2;
                $strand_hit1 = $strand{$transc{$hit1}};
                $strand_hit2 = $strand{$transc{$hit2}};
                if (($dist1 < 10000 and $dist1 > 0) or $dist2 < 10000 and $dist2 > 0) {
                    if ($strand_hit1 eq $strand_hit2) {
                        $hit1 =~ /TCONS_([0-9]*)/;
                        $code_hit1 = $1;
                        $hit2 =~ /TCONS_([0-9]*)/;
                        $code_hit2 = $1;
                        $substract = $code_hit1 - $code_hit2;
                        if ($substract == 1 or $substract == -1) {
                            $counter++;
                            print $fh $counter."\t".$hit1."\t".$hit2."\t".$key."\n";
                            print $counter."\t".$hit1."\t".$hit2."\t".$key."\n";
                        }
                    }
                }
            }
        }
    }
}

You can see, I have two print lines. One line prints to the screen and the other one prints to a file. However, the file remains empty. Why? I open the file in a correct way...

Do you have any idea what's happening here?

Thanks in advance.

Miller
  • 34,962
  • 4
  • 39
  • 60
user2886545
  • 711
  • 2
  • 8
  • 18
  • 1
    Are you looking at the file while the program is running and the handle is still open, or after it has finished? – CodeClown42 May 29 '14 at 11:05
  • 1
    [`tee`](http://en.wikipedia.org/wiki/Tee_%28command%29) can be used to clone STDOUT to a file without having to handle the file in your script, e.g. `perl your_script.pl | tee results_BLAST_adjacentgenes_samehit.txt` – RobEarl May 29 '14 at 11:08

1 Answers1

4

If you are looking at the file while it's running, the output is possibly buffered by the OS. You could test this by closing the filehandle immediately to see if it flushes the buffer. If that works and this delay is an issue for you, see this Q&A for recommendations about how to implement an unbuffered write.

Community
  • 1
  • 1
CodeClown42
  • 11,194
  • 1
  • 32
  • 67