another perl question

Kevin Buettner plug-discuss@lists.plug.phoenix.az.us
Thu, 23 May 2002 14:03:50 -0700


On May 23, 12:57pm, Roderick Ford wrote:

> I can't figure out from the books why this does not produce a hash element:
> 
> $addresses{$lineref->[0]} = ($#cmdNode >= 0 ? $#cmdNode : 0);

It is...

> perl -e '$lineref->[0] = "00401000"; $addresses{"$lineref->[0]"} = 
> (defined @cmdNode ? $#cmdNode : 0); print $#{keys %addresses};'

It's just that

    print $#{keys %addresses};

isn't doing what you think it is.  Try this instead...

    print scalar (keys %addresses)

..and I think you'll see the answer you expect (which is the number of
elements in the array).

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

Kevin