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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.