http://www.perlmonks.org?node_id=394736


in reply to How cand I find each dimension x,y,z of a 3D arrays

I made this subroutine for work so I guess it's more specific than the other two answers, but I find that this one can be modified and understood far easier than the others:

#this prints 0 print getSecondArraySize("la", %test)."\n"; # put stuff in $test{"la"}[0] = "1"; $test{"la"}[1] = "2"; $test{"la"}[2] = "3"; #this prints 3 print getSecondArraySize("la", %test)."\n";

And here's the code:

########################################################## # subroutine getSecondArraySize # Parameter: An associative array containing scalar # array and the name of a key # Returns the total amount of element(s) in the scalar # array within the associative array at the key location # index ########################################################## sub getSecondArraySize { # receives the argument my ($index, %array_to_count) = @_; # create the variable and get the number my $count=0; # determine the total amount of elements in the array while (defined($array_to_count{$index}[$count])) { $count++; } return $count; }