Re: How do I find the size of an array?⭐
by davorg (Chancellor) on Jun 13, 2000 at 14:33 UTC
|
The "value" of an array variable in any scalar context is its length. E.g.
scalar(@array)
Or
$length = @array;
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Either of the commands should do the job:
scalar(@array); $#array
Shashidhar Iddamsetty
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: How do I find the size of an array?
by nuance (Hermit) on Jun 13, 2000 at 14:39 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I find the size of an array?
by Anonymous Monk on Dec 06, 2001 at 19:04 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I find the size of an array?
by dsb (Chaplain) on Jan 24, 2001 at 19:47 UTC
|
Very simply. Evaluate the array in scalar context.
$size = @array;
Remember that the size of an array is not the same as the last element of an array. Since indexing of elements starts at zero, the last element of a ten-element array is 9. See the following code:
my @array = qw/1 2 3 4 5/;
print scalar @array, "\n"; # Size of array
print $#array, "\n"; # Last element of array
And the output:
5
4
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How do I find the size of an array?
by clash (Novice) on Dec 03, 2000 at 10:43 UTC
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How do I find the size of an array?
by ysth (Canon) on Nov 04, 2003 at 06:42 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I find the size of an array?
by panda.umesh9 (Initiate) on Feb 09, 2010 at 17:55 UTC
|
| [reply] [Watch: Dir/Any] |
|
$#array + 1 is wrong. $#array is the index of the last element in @array. It is *normally* one less than the number of elements in the array, but not always, as it assumes that array indices start at 0. If $[ is set then that is not necessarily the case.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
$length = @array;
For example:
#!/usr/bin/perl
use strict;
my @array = qw(one two three);
my $length = $#array + 1;
print "Dollar pound array = $length\n";
$length = @array;
print "At symbol array = $length\n";
__DATA__
{C} > test.pl
Dollar pound array = 3
At symbol array = 3
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
What's wrong with:
$length = @array;
Nothing. TIMTOWTDI.
-- Randal L. Schwartz, Perl hacker
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I find the size of an array?
by Anonymous Monk on Feb 09, 2004 at 04:58 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I find the size of an array?
by mattr (Curate) on Sep 03, 2005 at 15:36 UTC
|
I never like to hear the words "size of" since it is not the sizeof that a C programmer will want.
Also I am embarassed to say that I do not know or maybe have forgotten when it is that scalar @array -1 != $#array. For example an array with a single element at index 5 will give the same results for both. Or are we talking about some kind of pruning?
That said I hate evaluating arrays as a scalar anyway since I expect something bad could happen (tm). Probably the important information I promptly forgot was distilled to this inchoate fear. Say it isn't so!
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] |
Re: How do I find the size of an array?
by usless_blonde (Initiate) on Oct 14, 2003 at 12:53 UTC
|
Just to be reaaly stupid. What does $#array mean? I can't do that as everything after the # is ignored!
{QandAEditor note-- Summarizing a followup to this node: $#array is the index of the last element in @array. #, in this case, doesn't begin a comment.
$#array is not necessarily equal to scalar @array - 1. Use @array in scalar context to determine the size of an array. Use $#array when determining the index number of the last element of @array. }
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
$#array is the index of the last element in @array. In this case, the # character does NOT introduce a comment.
For example, if you have @array = qw(ant bat cat dog eagle fish ghoti) then $#array is 6 (the index of 'ghoti'). The number of elements in the array is usually $#array + 1.
Note that those values will change if $[ is set to anything other than 0. To cope with those times when someone has been naughty (use of $[ is strongly discouraged), you should get the number of elements in an array by evaluating @array in scalar context, such as:
print scalar @array;
or
$num_elements = @array;
| [reply] [Watch: Dir/Any] [d/l] [select] |