another perl question

Roderick Ford plug-discuss@lists.plug.phoenix.az.us
Fri, 24 May 2002 04:56:15 -0700


Yes, by printing out the last index (element of the array) you find the 
number of elements (or current index in a loop).  Additionally, when 
undefined (no elements) it returns "-1", which is what is happening in 
this case, since the key is not used to create an element.  However,  I 
haven't found that a string of digits is not allowed to be a hash key, 
which I think is actually the problem.  That is, thanks to Kevin who 
presented below the result from using strict refs.  ---rod

Kevin Buettner wrote:
> 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
> ________________________________________________
> See http://PLUG.phoenix.az.us/navigator-mail.shtml if your mail doesn't post to the list quickly and you use Netscape to write mail.
> 
> PLUG-discuss mailing list  -  PLUG-discuss@lists.plug.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss