Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: how many elements in array?

by moot (Chaplain)
on Apr 18, 2005 at 15:00 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found