On Mon, Dec 17, 2001 at 11:04:10PM -0500, Michael R. Wolf wrote:
> $length = @thearray
>
> The assignment operator supplies the scalar context.
No, it's not the assignment operator that provides the scalar context, it's
the assignment to a scalar.
Consider:
@anotherarray = @array;
or
Chris Spurgeon <[EMAIL PROTECTED]> writes:
> A zillion ways. One is
>
> $length = scalar(@thearray);
Which reduces to:
$length = @thearray
The assignment operator supplies the scalar context. The
scalar pseudo-function is therefore not needed.
OTOH
print scalar @thearray;
The scalar *is
"Agustin Rivera" <[EMAIL PROTECTED]> writes:
> print $#array;
Off by one. That prints the last index, not the number of
elements.
print scalar @array
If print isn't your use, try:
$length = @array;
$last_index = $#array;
--
Michael R. Wolf
All mammals learn by playing!
From: "Agustin Rivera" <[EMAIL PROTECTED]>
> print $#array;
>
> Agustin Rivera
This has been discussed on this list lately I believe.
This prints the max index of that array, not the array size.
Since normaly you index arrays in Perl from 0 the $#array is the
length-1, but it doesn't have to
just a note to add to this method
$#array gives the highest index, so the number of elements would be
$#array+1
Also, $#array is useful when doing for to go thru to complete array
since the first index is 0 and the last is nb of elements - 1
for(0..$#array) {#code here }
# or
foreach(@array) {
A zillion ways. One is
$length = scalar(@thearray);
Chris Spurgeon
Senior Design Technologist
[EMAIL PROTECTED]
ELECTRONIC INK
One South Broad Street
19th Floor
Philadelphia, PA 19107
www.electronicink.com
t 215.922.3800 x(233)
f 215.922.3880
-Original Message---
From: Ryan Guy <[EMAIL PROTECTED]>
> I need to know the fastest way to determine a length of an array. I
> know there is a built in kind of thing somewhere. I just cant
> remember it. Thanks
$length = scalar( @array );
Jenda
=== [EMAIL PROTECTED] == http://Jenda.Kry
Assigning the array to a scalar will give you a count:
push ( @initial_array, qw( apple orange bananna ));
$a = @initial_array;
print $a;
prints 3
-Original Message-
From: Ryan Guy [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 17, 2001 12:24 PM
To: '[EMAIL PROTECTED]'
Subject: Array
print $#array;
Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com
- Original Message -
From: "Ryan Guy" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, December 17, 2001 12:24 PM
Subject: Array Length.
> I need to know the fastest way to determine a length of a