another perl question
Kevin Buettner
plug-discuss@lists.plug.phoenix.az.us
Thu, 23 May 2002 14:19:50 -0700
On May 23, 2:03pm, Kevin Buettner wrote:
> I was puzzled about what exactly ``print $#{keys %addresses}'' is doing.
> What I think is happening is that ``keys %addresses'' is getting converted
> into a scalar and you're seeing the last array index of the array by that
> name. Here's an example which should make this somewhat more clear:
>
> $ perl -e '@aa = (1,2,3); print $#{'a' . 'a'}; print "\n";'
> 2
I've done a little more digging... $#{keys %addresses} is an example
of a "symbolic reference". (See the perlref man page for more
information.) It's instructive to put "use strict 'refs'" at the
beginning of Roderick's original example. E.g,
$ perl -e 'use strict "refs"; $lineref->[0] = "00401000"; $addresses{"$lineref->[0]"} =(defined @cmdNode ? $#cmdNode : 0); print $#{keys %addresses};'
Can't use string ("1") as an ARRAY ref while "strict refs" in use at -e line 1.
So, ``print $#{keys %addresses}'' is printing out the index of the last
element of the array named "1".
Kevin