At 04:36 PM 6/18/01 -0700, Peter Cornelius wrote:
>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,
What makes you think 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.
$hash{$key++} = $value
is equivalent to
$hash{$key} = $value;
$key = $key + 1;
and
$hash{++$key} = $value
is equivalent to
$key = $key + 1;
$hash{$key} = $value;
Okay?
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com