Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: how many elements in array?

by moot (Chaplain)
on Apr 18, 2005 at 15:00 UTC ( #448903=note: print w/replies, xml ) Need Help??

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 :/

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://448903]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2023-09-23 20:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found