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.
In reply to Re: problem with while loop
by GrandFather
in thread problem with while loop
by texuser74
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |