Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > question about data structures - what does $# mean?

Reply
Thread Tools

question about data structures - what does $# mean?

 
 
Ed
Guest
Posts: n/a
 
      08-30-2008
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
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      08-30-2008
Ed <(E-Mail Removed)> 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: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
 
 
 
Jrgen Exner
Guest
Posts: n/a
 
      08-30-2008
Ed <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
sanjeeb
Guest
Posts: n/a
 
      08-30-2008
On Aug 30, 5:21 pm, Ed <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      08-30-2008
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.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      08-30-2008
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.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic design question, about data structures JSeb C++ 4 02-12-2008 10:55 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Data structures question Shwetabh C Programming 5 08-01-2005 03:57 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
Data Structures Question... Saikrishna C++ 3 04-08-2004 02:32 PM



Advertisments