Your skill will accomplishwhat the force of many cannot PerlMonks

### How do I find the size of an array?

by vroom (His Eminence)
 on Jan 08, 2000 at 08:17 UTC Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

How do I find the size of an array?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
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;
Either of the commands should do the job: scalar(@array); \$#array Shashidhar Iddamsetty
wrong
Re: How do I find the size of an array?
by nuance (Hermit) on Jun 13, 2000 at 14:39 UTC
\$#array will return the last index of array @array.

Originally posted as a Categorized Answer.

Re: How do I find the size of an array?
by Anonymous Monk on Dec 06, 2001 at 19:04 UTC
When given a reference to an array... my \$size = scalar @{\$array_ref};

Originally posted as a Categorized Answer.

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.

Re: How do I find the size of an array?
by clash (Novice) on Dec 03, 2000 at 10:43 UTC
Yes, it will return the last index, but the counting starts at 0 so this might confuse some people if the information is public. If you wanted to use  \$#array and not confuse the user than add one to it..

\$var = \$#array + 1; or something of that sort.

Also, if you want to get the value of the last element in the array you can just use \$array[-1].

Originally posted as a Categorized Answer.

Re: How do I find the size of an array?
by ysth (Canon) on Nov 04, 2003 at 06:42 UTC
You can also get the last index of an array_ref:   my \$index = \$#{\$array_ref}

Originally posted as a Categorized Answer.

Re: How do I find the size of an array?
by Anonymous Monk on Feb 09, 2004 at 04:58 UTC
\$arraysize=@array; print"\$arraysize; #Try it!

Originally posted as a Categorized Answer.

Re: How do I find the size of an array?
by panda.umesh9 (Initiate) on Feb 09, 2010 at 17:55 UTC
\$length=(\$#array+1)
\$#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.

What's wrong with:

```\$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
What's wrong with:
```\$length = @array;
Nothing. TIMTOWTDI.

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.

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.

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

\$#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;

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-11-29 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite new Perl feature (in 2022) ...

Results (41 votes). Check out past polls.

Notices?