Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Length of 2-dimensional array

by c-era (Curate)
on Apr 06, 2001 at 00:33 UTC ( #70284=note: print w/ replies, xml ) Need Help??


in reply to Re: Length of 2-dimensional array
in thread Length of 2-dimensional array

This could also be written as:

my $a; # a 2d-array my $count=0; for (@$a){ $count += $#$_+1; } print "My 2D-array is $count long\n";


Comment on Re: Re: Length of 2-dimensional array
Download Code
Replies are listed 'Best First'.
Re: Re: Re: Length of 2-dimensional array
by extremely (Priest) on Apr 06, 2001 at 00:40 UTC
    my $a = [[1,2,3],[4,5,6,7],[3]]; my $count = 0; $count += @$_ for @$a;

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re:{3} Length of 2-dimensional array
by jeroenes (Priest) on Apr 06, 2001 at 00:55 UTC
    Yes. And extremely's reply spares even more keystrokes. Alls that inspires me to give a somewhat more useful answer:
    $a = [ [ 0..2 ], [0..10],[0..100] ]; my ($min, $max,$sum) = (undef, 0, 0); for (@$a){ $sum += my $a_length = @$_; $min = $a_length if $a_length < $min or not defined $min; $max = $a_length if $a_length > $max; } my $avg = $sum/ @$a; print "Avg: $avg; Min: $min; Max: $max\n";
    Gives some useful array statistics.

    Jeroen
    "We are not alone"(FZ)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2015-07-08 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls