# question about data structures - what does \$# mean?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Howdy all!

Here's a little program:

#!/usr/bin/perl -w
my \$d = ;
print \$#}."\n";

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

What's up with that?

As I read this, \$d is a ref to an anonymous assoc. array, which
contains one pair of values, named sid, with an anonymous array
containing one element: "lll".

So to get the number of elements in the array I try:
\$#}

But that does not work. It gives me 0.

Meanwhile, this does work:
scalar(@})

Which made me realize I don't really know what the # does when used in
\$#}.

Any help would be appreciated!

Thanks,

Ed

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

0 is the correct response.

perldata says:

\$days               # the simple scalar value "days"
\$days[28]           # the 29th element of array @days
\$days        # 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: http://joost.zeekat.nl/ | work: http://zeekat.nl /

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

Wrong operator. If you want the number of elements in an array then just
use the array in scalar context, if nothing else then by using scalar().

\$# OTOH will return the last index in that array, which is one less than
the number of elements unless someone messed around with \$[.

Just as it should.

Surprise, surprise.

jue

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

\$#array gives the last index  of the array not the number of elements
in the array, so you need to add 1 to \$#array to get the number of
elements.
Since you have only one element its giving 0 , if you put 3 elements
in array it will give 2

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

sanjeeb schreef:

For a Perl array, the number of elements is *normally* equal to the last
index plus one, but not *necessarily*.
Check out "\$[" in perlvar.

--
Affijn, Ruud

"Gewoon is een tijger."

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

Ed schreef:

Get rid of the "-w". Add:

use strict;
use warnings;

There is no need to concatenate, you can just write

print \$#{ \$d-> }, "\n";

Add a line with "\$[ = 1;" somewhere above it, and it will.
But you should really read perlvar first.

--
Affijn, Ruud

"Gewoon is een tijger."

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

Ed wrote:

\$# gives you the highest INDEX number of an array.  This array has one
item in it, so the highest index is zero (because arrays begin numbering
at zero).

If you want to find out how many elements are in the array you can use
the array variable in scalar context, or force scalar context with
scalar(), ie:

print scalar @};

--
David Filmer (http://DavidFilmer.com )
The best way to get a good answer is to ask a good question.