Array size

  • Thread starter David Dyer-Bennet
  • Start date
D

David Dyer-Bennet

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?
 
G

gnari

David Dyer-Bennet said:
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
 
M

Matija Papec

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
 
T

Tad McClellan

David Dyer-Bennet said:
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
 
D

David Dyer-Bennet

gnari said:
David Dyer-Bennet said:
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!
 
U

Uri Guttman

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
 
D

David Dyer-Bennet

Uri Guttman said:
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*.
 
U

Uri Guttman

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.
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
 
R

Richard Morse

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
 
A

Anno Siegel

[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
 
D

David Dyer-Bennet

Richard Morse said:
$ 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!
 
R

Richard Morse

David Dyer-Bennet said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top