in reply to how many elements in array?

All of the previous responses are correct in that they will tell you the size of an array (which is not quite what you asked for), however you may also be thinking that an element is any defined value, which does not necessarily give the same answer - a large array might be quite sparse (although an application of this escapes me right now).

my @a = (1, undef, 3); # Now scalar @a = 3, but there are only two elements of interest. # Brute force counting method: my $c = 0; $c++ for (grep { defined } @a); # $c now holds the number of defined elements, in this case 2.

Replies are listed 'Best First'.
Re^2: how many elements in array?
by tlm (Prior) on Apr 18, 2005 at 16:54 UTC

    Or simply

    my $c = grep defined, @a;

    the lowliest monk

      Ah, yes, or that :/