$hash{$key++}
> > > Wait a sec, brain cramp....
> > > Wouldn't that
> > > 1) just access $hash{$key}
> > > 2) increment $key
> > > 3) add $hash{$key + 1}
> >
> > I realize this is getting away from the original post, but I'm
confused by
> > #3 here. Wouldn't it just do 1 and 2? Would it actually access the
hash
> > twice?
>
> It would access the value retrieved by $key, then add 1 to $key, then
> create another entry in the hash table for $key + 1, unless it exists
> already. Doing ++$key would only do #2 and #3. Note this only works
if
> $key is a number
Not quite.
First, $key doesn't have to be a number. If it matches
[a-zA-Z]*[0-9]* then the ++ operator will increment it.
This is an intentional extra piece of perl magic.
Second, although it would create an entry in the hash
table for the incremented $key, that entry will often
vanish if it is not made use of (this is a good thing)
and will remain if it is made use of (this is also a good
thing). Unfortunately, it will sometimes remain even
though it is not made use of, as is seen in the following
code:
undef %hash;
exists $hash{a} and print '!'; # does not print.
exists $hash{a} and print '!'; # does not print.
exists $hash{a}{b} and print '!'; # does not print.
exists $hash{a} and print '!'; # prints !
exists $hash{a}{b} and print '!'; # does not print.
$key = 'a';
exists $hash{++$key} and print '!'; # does not print.
exists $hash{++$key}{b} and print '!'; # does not print.
exists $hash{c} and print '!'; # prints !
Basically, perl (as against Perl) is always a work in
progress, and this is one of the known problems that
will be addressed one day.