Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, My script works prefectly fine for an individual file, but when i try and and go through a list of files, the same answer is returned from the first file??? Script is long so have posted sections in detail:
open (LIST, "<all_res"); while ($output =<LIST>) { $file = 'test.pat' ; $file2 = $output ; $file3 = $output . ".res"; #PROCESS A COUPLE OF FILES open (COUNT, "<COMPARE"); while (<COUNT>) { ($i, $j) = split (/\s+/, $_); if ($i == 1) {$totalinterface++}; if ($i == 0) {$totalnon++}; if ($j >= 0.5 && $i == 1) {$TP++ unless $j == 1 && $i == 1}; if ($j >= 0.5 && $i == 0) {$FP++}; if ($j <=0.499999 && $i == 1) {$FN++}; if ($j <=0.499999 && $i == 0) {$TN++}; $total = $TP+$FP+$FN+$TN; } #PRINT VALUES TO FILE } PROCEED ONTO NEXT FILE IN LIST

Replies are listed 'Best First'.
Re: while loop returns same values
by muntfish (Chaplain) on Sep 14, 2004 at 11:21 UTC

    You haven't said what you're trying to achieve, but it looks like you are simply opening the same file (called COMPARE) multiple times. Having set $output from the "all_res" file, you don't make use of its value (or any of the $file variables.

    If you could tell us what you are expecting the program to do, it might help someone work out why it is not doing that.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
      The $file (always the same) and $file2 (taken from a list of files to loop through) are used to make a file COMPARE. Compare just lists 2 columns of data $i and $j. Then print those results $FN etc to an output file.

        Hmm... in that case, it looks about right. But a few things spring to mind:

        You don't remove the trailing line-end from $output. You should do something like:

        while ($output = <LIST>) { chomp $output; $file = 'test.pat'; $file2 = $output; $file3 = "$output.res"; ...

        You haven't shown the code that creates the COMPARE file. Is it possible that it fails after the first run, so you always pick up the same COMPARE file? Maybe it fails if COMPARE already exists?

        When you open the COMPARE file, you should check whether the open() is actually successful, too:

        open (COUNT, "<COMPARE") or die "Can't open COMPARE: $!";

        There are other things you could do which might help you find the problem, such as use strict; use warnings;

        I hope this helps a little. Let us know how you get on.


        s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&