Re: question about data structures - what does $# mean?

Ed <edwardjameshartnett@xxxxxxxxx> writes:

Howdy all!

Here's a little program:

#!/usr/bin/perl -w
my $d = {sid=>["lll"]};
print $#{$d->{sid}}."\n";

I expect this to print 1, but it prints 0.

0 is the correct response.

perldata says:

$days # the simple scalar value "days"
$days[28] # the 29th element of array @days
$days{’Feb’} # the ’Feb’ value from hash %days
$#days # the last index of array @days

Since perl arrays are indexed starting at 0 by default, an array
containing 1 element has a "last index" value of 0.

As you noted, the correct way to find out the length of an array is to
use scalar(@array).

Joost Diepenmaat | blog: | work: