in reply to Re: Re: Incrementing a Hash Value
in thread Incrementing a Hash Value

Is it undefined, or does do this: $k = $i; $i++; $i = $k;

Both.

$i++ stores the old value, increases the value, and then sets the return value to the stored old value. Although that is true for all versions of Perl, its behaviour is not documented. Actually, it is documented, but wrong:

[...] increment or decrement the variable after returning the value. [...] -- perlop
It doesn't increment after returning. It increments first, and then returns the old value.

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

Replies are listed 'Best First'.
Re: Incrementing a Hash Value
by Abigail-II (Bishop) on Jun 14, 2002 at 15:51 UTC
    It doesn't increment after returning. It increments first, and then returns the old value.
    No, and that's the entire problem. You do not know when the increment will happen, that is specifically left undefined. (It was left undefined in C to give implementers of compilers all sorts of freedom to optimize it - idem for Perl). What a specific version of Perl does not gives you absolutely no garantee what it will do in a next version.

    Abigail

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Re: Re: Re: Incrementing a Hash Value
by marvell (Pilgrim) on Jun 14, 2002 at 15:42 UTC
    That sounds well defined to me. If it has a predicible behaviour, even if it's not useful, it's defined.

    I must be missing something.

    --
    Steve Marvell

      But it isn't well defined. In
      $i = $i ++;
      you do not know whether the assignment will happen before, or after the increment.

      Abigail

        There is no good reason why that is undefined, if perl has one source code and C has no eval type function.

        --
        Steve Marvell