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

In reply to Re: problem with while loop by GrandFather
in thread problem with while loop by texuser74

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.