### How to find size of array in array?

 on Nov 17, 2001 at 00:22 UTC Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

```@a = ("Zero","One","Two");
@b = ("A","B");
\$c[0]= \@a;
\$c[1]= \@b;
\$asize = @a;          # just fine for scalar array
\$bsize = @c[1][0];    # but what to do here?
print "Array a size is:       \$asize\n";
print "Array b size in c is:  \$bsize\n";

# What is syntax to find the size of array b in array c?

Edit Masem 2001-11-16 - CODE tags

Replies are listed 'Best First'.
Re: How to find size of array in array?
by kwoff (Friar) on Nov 17, 2001 at 00:43 UTC
```\$bsize = @{\$c[1]};
(crazyinsomniac) Re: How to find size of array in array?
by crazyinsomniac (Prior) on Nov 17, 2001 at 16:26 UTC
Re: How to find size of array in array?
by jbert (Priest) on Nov 19, 2001 at 19:35 UTC
Good advice in the other responses. Some more words for you:

Your line of code to find the length:

```  \$asize = @a;
works because an array evaluates to the number of elements it contains in a scalar context. Assigning to a scalar (as you do above) provides a scalar context to the right hand side (RHS). So the array is evaluated in this scalar context, giving the length, which the assignment then puts into your '\$asize' variable.

Why did I bother spelling this out in a long-winded way? Because it is useful to understand what it going on. For example, I usually write the above as:

```  my \$asize = scalar @a;
The 'scalar' keyword supplies a scalar context to its argument and gives the result of the evaluation. It is redundant in the above example (because we already have a scalar context from the assignment to the scalar).

So why do I write it like that? Because:

• It is an explicit statement of what I am doing. I consider that more readable.
• I can use the same syntax every place I need an array size. Look at the following:
```#!/usr/local/bin/perl -w
use strict;

my @array = qw( 1 2 3 4 5 );
my \$asize = scalar @array;
print "Array size 1 is ", \$asize, "\n";
print "Array size 2 is ", @array, "\n";
print "Array size 3 is ", scalar @array, "\n";
I *need* to use the scalar keyword here, because otherwise the 'print' command gives a list context to the array, which lists all the elements instead of giving the size.

Since I sometimes get cut-and-paste happy, it is nice to have code which is explicit about what context it wants.

And I'll conclude this overly-pedantic missive to state that you should look into using 'use strict' and '#!... -w', to force you into good habits. (Heh...habits! Monks! How funny is that!? (Hrrrm...the first couple of million times were probably worth chuckling at, but thats about it))
For getting the size of an array @array, you can just use the variable as \$#array. Shashidhar Iddamsetty

Create A New User
Node Status?
node history
Node Type: perlquestion [id://125905]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2020-05-30 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
If programming languages were movie genres, Perl would be:

Results (171 votes). Check out past polls.

Notices?