http://www.perlmonks.org?node_id=126279


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

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:

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))

Replies are listed 'Best First'.
Re^2: How to find size of array in array?
by Anonymous Monk on Apr 23, 2007 at 03:43 UTC
    For getting the size of an array @array, you can just use the variable as $#array. Shashidhar Iddamsetty