in reply to problem with while loop

There is a bunch of stuff you ought to do to tidy up your code. As already mentioned making your variables local to the sub will help the immediate problem. But a little restructuring will help with diagnostics when things go pear shaped. Consider:

use strict; use warnings; sub compare { my ($log, $olog) = @_; my %logHash; open my $XR, '<', $log || die "Failed to open $log: $!"; $logHash{$_}++ while <$XR>; close $XR; open my $OXR, '<', $olog || die "Failed to open $log: $!"; $logHash{$_}-- while <$OXR>; close $OXR; my $diffs = 0; for my $match (sort keys %logHash) { next unless $logHash{$match}; if ($logHash{$match} > 0) { print "Line missing from $olog: $match\n" if $diffs++ < 10 +; } elsif ($logHash{$match} < 0) { print "Line missing from $log: $match\n" if $diffs++ < 10; } } return $diffs; }

which uses the three parameter version of open to provide a higher degree of safety. A sensible error message is provided by die so failure is easier to diagnose if an open doesn't work.

Lines are counted "in" and "out" and the first few per iteration are reported so you can check progress.

Lexical file handles are used as a matter of good practice.


Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re^2: problem with while loop
by texuser74 (Monk) on Aug 01, 2008 at 04:40 UTC
    GrandFather, thanks for tidying up my script. Now it works fine and safely. Thanks for your support.
Re^2: problem with while loop
by Anonymous Monk on Aug 01, 2008 at 04:36 UTC
    #!/usr/bin/perl -- use strict; use warnings; use File::Copy; use File::Compare; my @latex = qw'latex test.tex'; # !!perldoc -f system system(@latex) == 0 or die "system (@latex) failed:( $! ):( $? )"; copy(qw"test.log test-old.log") or die "Copy failed: $!"; system(@latex) == 0 or die "system (@latex) failed:( $! ):( $? )"; while ( compare( "test.log", "test-old.log" ) ) { copy(qw"test.log test-old.log") or die "Copy failed: $!"; system(@latex) == 0 or die "system (@latex) failed:( $! ):( $? )"; } exit 0;
    Note that File::Compare uses 2 arg open