Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Count elements in multidimensional arrays

by klorentzen (Initiate)
on May 24, 2019 at 23:47 UTC ( #11100500=perlquestion: print w/replies, xml ) Need Help??

klorentzen has asked for the wisdom of the Perl Monks concerning the following question:

If I create a multidimensional array using the following:

#!/usr/bin/perl my @ar1; $ar1[0][0] = "one"; $ar1[0][1] = "two"; $ar1[1][0] = "three"; $ar1[1][1] = "four"; $ar1[1][2] = "five"; my $count = scalar @{ $ar1[1] }; print "count = $count\n";

./ar1

count = 3

However, if I create the array this way it doesn't work: #!/usr/bin/perl my @ar1; $ar1 = (["one", "two"], ["three", "four", "five"]); my $count = scalar @{ $ar1[1] }; print "count = $count\n";

./ar1

count =

What is the correct way to access / count the array elements using the array creation method in the second code segment?

Thanks, ...Kurt

Replies are listed 'Best First'.
Re: Count elements in multidimensional arrays
by Paladin (Vicar) on May 25, 2019 at 00:00 UTC
    In the second one you are assigning to $ar1 which isn't @ar1. If you used strict and warnings you would have been warned about this.
Re: Count elements in multidimensional arrays
by BillKSmith (Monsignor) on May 25, 2019 at 12:23 UTC
    Paladin has already shown that you could have found your error yourself if you had used the pragmas "strict" and "warnings". Also note that a little extra whitespace to align your array definition can make it easier to read. That alone can help to avoid errors in accessing them.
    use strict; use warnings; my @ar1 = ( ["one", "two", ], ["three", "four", "five", ], ); my $count = scalar @{ $ar1[1] }; print "count = $count\n";
    Bill
Re: Count elements in multidimensional arrays
by Marshall (Canon) on May 26, 2019 at 01:40 UTC
    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: perlquestion [id://11100500]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2022-06-28 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (92 votes). Check out past polls.

    Notices?