Array size

Discussion in 'Perl Misc' started by David Dyer-Bennet, Feb 9, 2004.

  1. I know $#foo is the max subscript of @foo.

    What's the max subscript of $f = [5, 4, 5, 4, 9]? (Yeah, I know, it's
    4).

    That is, if I have an array ref, how do I get the max subscript of
    that array, without copying the whole thing to a temporary array?
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 9, 2004
    #1
    1. Advertising

  2. David Dyer-Bennet

    gnari Guest

    "David Dyer-Bennet" <> wrote in message
    news:-b.net...
    > I know $#foo is the max subscript of @foo.
    >
    > What's the max subscript of $f = [5, 4, 5, 4, 9]? (Yeah, I know, it's
    > 4).
    >
    > That is, if I have an array ref, how do I get the max subscript of
    > that array, without copying the whole thing to a temporary array?


    did you try $#$f ?

    gnari
     
    gnari, Feb 9, 2004
    #2
    1. Advertising

  3. David Dyer-Bennet

    Matija Papec Guest

    On Mon, 09 Feb 2004 01:32:15 -0600, David Dyer-Bennet <>
    wrote:

    >I know $#foo is the max subscript of @foo.
    >
    >What's the max subscript of $f = [5, 4, 5, 4, 9]? (Yeah, I know, it's
    >4).
    >
    >That is, if I have an array ref, how do I get the max subscript of
    >that array, without copying the whole thing to a temporary array?


    $#{ $f } or just $#$f
     
    Matija Papec, Feb 9, 2004
    #3
  4. David Dyer-Bennet <> wrote:
    > I know $#foo is the max subscript of @foo.
    >
    > What's the max subscript of $f = [5, 4, 5, 4, 9]? (Yeah, I know, it's
    > 4).
    >
    > That is, if I have an array ref, how do I get the max subscript of
    > that array, without copying the whole thing to a temporary array?



    Apply "Use Rule 1" from perlreftut.pod:

    my $last_i = $#foo; # pretend it is a plain array

    my $last_i = $#{ }; # replace the array _name_ with a block...

    my $last_i = $#{ $f }; # that returns a reference to an array


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 9, 2004
    #4
  5. "gnari" <> writes:

    > "David Dyer-Bennet" <> wrote in message
    > news:-b.net...
    >> I know $#foo is the max subscript of @foo.
    >>
    >> What's the max subscript of $f = [5, 4, 5, 4, 9]? (Yeah, I know, it's
    >> 4).
    >>
    >> That is, if I have an array ref, how do I get the max subscript of
    >> that array, without copying the whole thing to a temporary array?

    >
    > did you try $#$f ?


    No, but I'm quite sure it's not in the man pages anywhere, because I
    looked at all the $# hits, and didn't find it.

    Thanks to you and the others who provided the answer!
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 9, 2004
    #5
  6. David Dyer-Bennet

    Uri Guttman Guest

    >>>>> "DD" == David Dyer-Bennet <> writes:

    >> did you try $#$f ?


    DD> No, but I'm quite sure it's not in the man pages anywhere, because I
    DD> looked at all the $# hits, and didn't find it.

    because it has nothing to do with array size but more to do with
    references. read perlreftut and perlref.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 9, 2004
    #6
  7. Uri Guttman <> writes:

    >>>>>> "DD" == David Dyer-Bennet <> writes:

    >
    > >> did you try $#$f ?

    >
    > DD> No, but I'm quite sure it's not in the man pages anywhere, because I
    > DD> looked at all the $# hits, and didn't find it.
    >
    > because it has nothing to do with array size but more to do with
    > references. read perlreftut and perlref.


    Yes, read them, too. None of the man pages give a clue about how to
    apply $# to a reference. At least not a clue that can be *found*.
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 10, 2004
    #7
  8. David Dyer-Bennet

    Uri Guttman Guest

    >>>>> "DD" == David Dyer-Bennet <> writes:

    DD> Uri Guttman <> writes:
    >>>>>>> "DD" == David Dyer-Bennet <> writes:

    >>
    >> >> did you try $#$f ?

    >>

    DD> No, but I'm quite sure it's not in the man pages anywhere, because I
    DD> looked at all the $# hits, and didn't find it.
    >>
    >> because it has nothing to do with array size but more to do with
    >> references. read perlreftut and perlref.


    DD> Yes, read them, too. None of the man pages give a clue about how to
    DD> apply $# to a reference. At least not a clue that can be *found*.

    no, it tells you how to do ANYTHING with a ref. see tad's recent posts
    showing the rule for converting a regular variable use into a ref
    use. same thing for $#

    you have to use a little brain power with the docs as with anything to
    do with coding. they can't spell out every possible combination of
    everything in everyplace.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 10, 2004
    #8
  9. In article <-b.net>,
    David Dyer-Bennet <> wrote:
    > "gnari" <> writes:
    > > did you try $#$f ?

    >
    > No, but I'm quite sure it's not in the man pages anywhere, because I
    > looked at all the $# hits, and didn't find it.


    $ perldoc perlintro

    ....
    /array
    /

    > An array represents a list of values:
    >
    > my @animals = ("camel", "llama", "owl");
    > my @numbers = (23, 42, 69);
    > my @mixed = ("camel", 42, 1.23);
    >
    > Arrays are zero-indexed. Here's how you get at elements in an
    > array:
    >
    > print $animals[0]; # prints "camel"
    > print $animals[1]; # prints "llama"
    >
    > The special variable $#array tells you the index of the last ele-
    > ment of an array:
    >
    > print $mixed[$#mixed]; # last element, prints 1.23
    >
    > You might be tempted to use "$#array + 1" to tell you how many
    > items there are in an array. Don't bother. As it happens, using
    > @array where Perl expects to find a scalar value ("in scalar con-
    > text") will give you the number of elements in the array:


    I'm assuming you're using less as your pager.

    Also, page 76 in the Camel book (3rd ed).

    HTH,
    Ricky
     
    Richard Morse, Feb 10, 2004
    #9
  10. David Dyer-Bennet

    Anno Siegel Guest

    David Dyer-Bennet <> wrote in comp.lang.perl.misc:

    [perlreftut and perlref]

    > Yes, read them, too. None of the man pages give a clue about how to
    > apply $# to a reference. At least not a clue that can be *found*.


    That would in part depend on who does the searching, wouldn't it?

    Anno
     
    Anno Siegel, Feb 10, 2004
    #10
  11. Richard Morse <> writes:

    > In article <-b.net>,
    > David Dyer-Bennet <> wrote:
    >> "gnari" <> writes:
    >> > did you try $#$f ?

    >>
    >> No, but I'm quite sure it's not in the man pages anywhere, because I
    >> looked at all the $# hits, and didn't find it.

    >
    > $ perldoc perlintro


    etc. Yes, I know there's doc on $#arrayname; plenty of it. It was
    trying to apply it to a reference that I asked about, and was
    talking. It's really better to understand the discussion before
    jumping into the middle!
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 11, 2004
    #11
  12. In article <-b.net>,
    David Dyer-Bennet <> wrote:

    > Richard Morse <> writes:
    > > $ perldoc perlintro

    >
    > etc. Yes, I know there's doc on $#arrayname; plenty of it. It was
    > trying to apply it to a reference that I asked about, and was
    > talking. It's really better to understand the discussion before
    > jumping into the middle!


    Mea culpa.
     
    Richard Morse, Feb 11, 2004
    #12
    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. Zygmunt Krynicki
    Replies:
    1
    Views:
    614
    Ivan Vecerina
    Oct 11, 2003
  2. Denis Palas
    Replies:
    2
    Views:
    673
    Daniel Pitts
    Dec 19, 2006
  3. Replies:
    5
    Views:
    550
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    506
    Bob Hairgrove
    Apr 10, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,587
    Michael Jung
    May 25, 2008
Loading...

Share This Page