in reply to Re: while loop returns same values
in thread while loop returns same values

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.

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

    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$&
      after each loop i unlink COMPARE, i have checked both the contents and altered to die if unable to open with no success!
      thought i would attach the enitre code
      open (LIST, "<all_res"); while ($output =<LIST>) { chomp $output; $file = 'test.pat' ; $file2 = $output ; $file3 = $output . ".resauto"; open (FILE, "<$file"); open (OUT, ">TRUE_OUTPUTS"); open (FILE2, "<$file2"); open (OUT2, ">COMPARE"); while (<FILE>) { if ($_ =~ m/^\s\s\s\s+(\d)\s+$/) {print OUT "$1\n"} } close (FILE); close (OUT); while (<FILE2>) { push @col, $_ =~ m/^(\S+)/ unless ...; } close(FILE2); open(COL, "<TRUE_OUTPUTS"); while(<COL>) { $_ =~ s/^(\S)/$1 $col[$i++]/; print OUT2 $_; } close(COL); close(OUT2); open (COUNT, "<COMPARE") or "Can't open 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++}; #if ($j >= 0.5001 ) {$TP++}; $total = $TP+$FP+$FN+$TN; } close(COUNT); open (completeout, ">$file3"); print completeout "$file2\n"; print completeout "$TP\n"; print completeout "$TN\n"; print completeout "$totalinterface\n"; print completeout "$totalnon\n"; unlink "TRUE_OUTPUTS"; unlink "COMPARE"; }
        I think I see some problems. In

        while (<FILE2>) { push @col, $_ =~ m/^(\S+)/ unless ...; }

        you probably want to say my @col, otherwise @col keeps accumulating stuff from the previous pass through the first while loop.

        Likewise, in

        while(<COL>) { $_ =~ s/^(\S)/$1 $col[$i++]/; print OUT2 $_; }

        you probably want to say my $i++

        so $i starts at 0 again, instead of being left with whats in it from the while (<COUNT>) loop.

        And again, in

        while (<COUNT>){ ($i, $j) = ...

        it would be good if you used

        my ($i, $j)

        You also create and destroy the TRUE_OUTPUTS file each time through the loop. But from what you've shown, it never changes. So it would be better to put this at the top:

        open (FILE, "test.pat") or die "Can't open test.pat: $!"; open (OUT, ">TRUE_OUTPUTS") or die "Can't open TRUE_OUTPUTS: $!"; while (<FILE>) { if ($_ =~ m/^\s{4,}(\d)\s+$/) {print OUT "$1\n"} } close (FILE); close (OUT);

        Note that I used a constant "test.pat" since you said that doesn't change. I also added die and tidied up the regex by using {4,} to match 4 or more.

        Also, you forgot the die in

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

        And now, just to be nitpicky, $output seems like a bad variable name. I think $input_file or just $input would be better.

        Hope this helps.

        TheEnigma