I'm sorry to drag this thread on for something that's completely academic,
but I can't help myself.  I think it's been established that if you find
yourself doing this $key++ thing you are probably using the wrong data
structure (i.e. a hash when you want an array), but let me just throw up
some test code and it's output for everyone to consider...

-----
use Data::Dumper;

%hash = (
        1 => "I exist...",
        2 => "This is academic",
        3 => "I should be using an array"
        );

print Dumper \%hash;

for $key (keys %hash) {
        $hash{$key++}="Redefined";
        print "$key => $hash{$key}\n";
}

print Dumper \%hash;

__output__ 
$VAR1 = {
          '1' => 'I exist...',
          '2' => 'This is academic',
          '3' => 'I should be using an array'
        };
2 => This is academic
3 => I should be using an array
4 => 
$VAR1 = {
          '1' => 'Redefined',
          '2' => 'Redefined',
          '3' => 'Redefined'
        };

---Comments--
So the $hash{4} never gets autovivified, even though it's dereferenced, And
it's pretty clear that the increment happens after the assignment, like you
should expect.  Also, if you use the prefix version of '++' it does assign
to $hash{4} so it does get autovivified (I just like typing that word ;)
This all is starting to seem obvious but Paul's response to the original
post kinda set me spinning.

Maybe this is what you were all saying anyway but it helped clarify it for
Me, that is me, i mean, er... well...

Peter C.

Reply via email to