Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Find number of items in a field multi-demensional array

by stevieb (Canon)
on Apr 26, 2018 at 18:45 UTC ( [id://1213628]=note: print w/replies, xml ) Need Help??


in reply to Find number of items in a field of a multi-demensional array

You need to iterate over the top-level array, and then work on each element individually. Here's an example:

use warnings; use strict; my @aoa = ( [qw(1 1 1 1)], [qw(2 2 2)], 'a string', [qw(3 3 3 3 3 3 3)], {a => 1, b => 2}, ); my $index = 0; for (@aoa){ if (ref $_ eq 'ARRAY'){ my $element_count = scalar @$_; print "elem $index has $element_count elements\n"; } else { print "elem $index is not an array reference\n"; } $index++; }

Output:

elem 0 has 4 elements elem 1 has 3 elements elem 2 is not an array reference elem 3 has 7 elements elem 4 is not an array reference

Log In?
Username:
Password:

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

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

    No recent polls found