question about data structures - what does $# mean?

E

Ed

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.

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:
$#{$d->{sid}}

But that does not work. It gives me 0.

Meanwhile, this does work:
scalar(@{$d->{sid}})

Which made me realize I don't really know what the # does when used in
$#{$d->{sid}}.

Any help would be appreciated!

Thanks,

Ed
 
J

Joost Diepenmaat

Ed said:
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).
 
J

Jürgen Exner

Ed said:
So to get the number of elements in the array I try:
$#{$d->{sid}}

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 $[.
But that does not work. It gives me 0.

Just as it should.
Meanwhile, this does work:
scalar(@{$d->{sid}})

Surprise, surprise.

jue
 
S

sanjeeb

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.

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:
$#{$d->{sid}}

But that does not work. It gives me 0.

Meanwhile, this does work:
scalar(@{$d->{sid}})

Which made me realize I don't really know what the # does when used in
$#{$d->{sid}}.

Any help would be appreciated!

Thanks,

Ed
$#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
 
D

Dr.Ruud

sanjeeb schreef:
$#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.

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

Dr.Ruud

Ed schreef:
#!/usr/bin/perl -w

Get rid of the "-w". Add:

use strict;
use warnings;

my $d = {sid=>["lll"]};
print $#{$d->{sid}}."\n";

There is no need to concatenate, you can just write

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

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top