question about data structures - what does $# mean?

Discussion in 'Perl Misc' started by Ed, Aug 30, 2008.

  1. Ed

    Ed Guest

    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
    Ed, Aug 30, 2008
    #1
    1. Advertising

  2. Ed <> 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/
    Joost Diepenmaat, Aug 30, 2008
    #2
    1. Advertising

  3. Ed <> 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
    Jürgen Exner, Aug 30, 2008
    #3
  4. Ed

    sanjeeb Guest

    On Aug 30, 5:21 pm, Ed <> 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
    sanjeeb, Aug 30, 2008
    #4
  5. Ed

    Dr.Ruud Guest

    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."
    Dr.Ruud, Aug 30, 2008
    #5
  6. Ed

    Dr.Ruud Guest

    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."
    Dr.Ruud, Aug 30, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,755
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    696
    Emmanuel Delahaye
    Sep 24, 2005
  3. Li Ma
    Replies:
    1
    Views:
    2,247
    Roedy Green
    Mar 9, 2009
  4. Rahul
    Replies:
    4
    Views:
    539
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,166
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page