Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Array size

Reply
Thread Tools

Array size

 
 
David Dyer-Bennet
Guest
Posts: n/a
 
      02-09-2004
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, <(E-Mail Removed)>, <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/>
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      02-09-2004
"David Dyer-Bennet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-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




 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      02-09-2004
On Mon, 09 Feb 2004 01:32:15 -0600, David Dyer-Bennet <(E-Mail Removed)>
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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-09-2004
David Dyer-Bennet <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
David Dyer-Bennet
Guest
Posts: n/a
 
      02-09-2004
"gnari" <(E-Mail Removed)> writes:

> "David Dyer-Bennet" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)-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, <(E-Mail Removed)>, <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/>
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-09-2004
>>>>> "DD" == David Dyer-Bennet <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
David Dyer-Bennet
Guest
Posts: n/a
 
      02-10-2004
Uri Guttman <(E-Mail Removed)> writes:

>>>>>> "DD" == David Dyer-Bennet <(E-Mail Removed)> 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, <(E-Mail Removed)>, <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/>
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-10-2004
>>>>> "DD" == David Dyer-Bennet <(E-Mail Removed)> writes:

DD> Uri Guttman <(E-Mail Removed)> writes:
>>>>>>> "DD" == David Dyer-Bennet <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Richard Morse
Guest
Posts: n/a
 
      02-10-2004
In article <(E-Mail Removed)-b.net>,
David Dyer-Bennet <(E-Mail Removed)> wrote:
> "gnari" <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-10-2004
David Dyer-Bennet <(E-Mail Removed)> 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
 
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
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
On template method to obtain the size of an array. Was: [OT] Re: 2 stupid array questions!! Zygmunt Krynicki C++ 1 10-11-2003 01:18 AM



Advertisments