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";
This would work fine with the pre-increment operator:
DB<3> ++$i and print "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.