Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Count elements in multidimensional arrays

by Marshall (Canon)
on May 26, 2019 at 01:40 UTC ( [id://11100545]=note: print w/replies, xml ) Need Help??


in reply to Count elements in multidimensional arrays

In Perl, a 2-D array is an array of references to 1-D arrays.
Look at Data Type: Array in the Tutorials section of Perl Monks.
A Perl 2-D array can have a different number of "columns" on each "row".
#!/usr/bin/perl use strict; use warnings; my @ar1 = (["one", "two"], ["three", "four", "five"]); my $count = scalar @{ $ar1[1] }; print "count: Elements in 2nd Row = $count\n"; print "number of dimensions =", scalar @ar1, "\n"; my $total_elements; foreach my $row_ref (@ar1) { $total_elements += @$row_ref; } print "total elements in array:ar1=$total_elements\n"; __END__ count: Elements in 2nd Row = 3 number of dimensions =2 total elements in array:ar1=5

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found