The reason of the problem is that when you run:
$H_count ++ and ...
for the first time, the post increment operator sets $H_count to 1 and returns 0, i.e. a false value. Therefore, the statement following the and operator is not executed. The next time you run the same post-increment statement, it will return 1 (and subsequently other true values) and it will work fine as shown in the following test under the Perl debugger:
DB<1> $h++ and print "foo"; DB<2> $h++ and print "foo"; foo
This would work fine with the pre-increment operator:
DB<3> ++$i and print "foo"; foo
The solution suggested by swl is probably better because there is no hidden surprise in it.

As a side comment, please note that in these two code lines:

$line =~ s/\r//g; # removes windows CR characters $line =~ s/\s+$//; # removes trailing white spaces
the first line isn't useful, because the second code line will remove all trailing white spaces, including the \r Windows CR character.

In reply to Re^2: List comparison problem by Laurent_R
in thread List comparison problem by perlmonkster

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.