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: http://joost.zeekat.nl/ | work: http://zeekat.nl/
.



Relevant Pages

  • Re: use strict; use warnings;
    ... Perl arrays already know how long they are: you don't need to count for ... You may not have covered this, but using an array in scalar ... Originally I thought they wanted to see the reshuffled deck and print the ... top 5 cards. ...
    (comp.lang.perl.misc)
  • Re: kindergarten array vs. for question
    ... > I read perldata and perllol. ... > array" gets printed twice instead of six times, ...
    (comp.lang.perl.misc)
  • what does foreach do?
    ... So I've seen code like this ... What's the internal structure of Perl arrays? ... I'm curious about how much time it takes to do something like insert into the middle ofan array. ...
    (perl.beginners)
  • Re: set a variable with a specified element of an array...and all elements that follows
    ... gniagnia wrote in comp.lang.perl.misc: ... My array @array contains AT LEAST 7 elements. ... that's an array slice (see perldata). ...
    (comp.lang.perl.misc)