in reply to Re: Looping through a file, reading each line, and adding keys/editing values of a hash
in thread Looping through a file, reading each line, and adding keys/editing values of a hash

Why use a post-increment instead of a pre-increment when the value is not being used? $gene_count{ $row[18] }++ is (imo) better written ++$gene_count{$row[18]} so the increment is obvious.

True laziness is hard work
  • Comment on Re^2: Looping through a file, reading each line, and adding keys/editing values of a hash
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Looping through a file, reading each line, and adding keys/editing values of a hash
by Kenosis (Priest) on Dec 06, 2013 at 16:48 UTC

    Interesting question.

    One reason is that the OP already attempted a post-increment, and since the two increment types would produce the same outcome, why make the change?

    Another reason is my personal preference for this counting situation. If, for example, I were on a sidewalk, tallying all the red cars that passed by, I wouldn't make a tally mark upon their approach (pre-increment), but rather after they crossed an imaginary line extending across the street from my position (post-increment).

    Perhaps this ultimately boils down to personal preference, in cases like these...

Re^3: Looping through a file, reading each line, and adding keys/editing values of a hash
by Anonymous Monk on Dec 05, 2013 at 20:16 UTC
    There are different schools of thought there. I strongly favour the postincrement.

      I know its strongly favoured by many. I don't understand why.

      My strong preference is to use pre-increment where possible so the increment operator is more easily seen (trailing stuff is more easily ignored). But maybe I'm missing something important about the post-increment?

      A very minor consideration may be that the post-increment could be slower for some implementations that the pre-increment. The difference is so slight that it would be exceptionally unusual for that to be a consideration.

      True laziness is hard work
        In void context, Perl agrees with you:
        $ perl -MO=Deparse -e '$x++' ++$x;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ