in reply to Incrementing a Hash Value

marvell has a correct answer for you. I wanted to explain for you why your choice was not working.
// I am making the assumption that the syntax // error here is just a typo ( missing a $ in front // of the second thes_name) . $thes_name{$fields[1]} = thes_name{$fields[1]}++;

What this code does is effectively the following:
Start) $thes_name{$fields[1]} has a value of 1
1) Resolves $thes_name{$fields[1]} to its value "1".
2) Increments $thes_name{$fields[1]} to "2"
3) Sets $thes_name{$fields[1]} to the previously resolved value, "1".
End) Thus $thes_name always stays at "1".

Because you used a "post-increment" it happens after the variable is already resolved. If you had used a "pre-increment", ++$thes_name{$fields[1]}, if would have done what you expected -- but marvells answer for that is better.

Replies are listed 'Best First'.
Re: Incrementing a Hash Value
by Abigail-II (Bishop) on Jun 14, 2002 at 12:04 UTC
    It's a common mistake to assume that it works this way. And with some compilers and some versions of Perl it will work on some platforms for some values of the hash. But in other cases, pink monkeys may fly out of your nose, your dog may die and your piano suddenly walks away.

    The behaviour of $i = $i ++ in all its variants is undefined. Anything could happen.

    Abigail

      And with some compilers and some versions of Perl it will work on some platforms for some values of the hash.

      Compilers and platforms have absolutely nothing to do with the order in which Perl walks its ops.

      Perl code (code written in Perl) is not compiled by a C compiler.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

    A reply falls below the community's threshold of quality. You may see it by logging in.