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; }

Replies are listed 'Best First'.
Re: Answer: How cand I find each dimension x,y,z of a 3D arrays
by Velaki (Chaplain) on Sep 29, 2004 at 17:33 UTC

    Just curious, but couldn't this be accomplished with

    $#{$array_to_count{$index}}+1
    as well, instead of using the while loop?

    That way the code would be

    sub getSecondArraySize { # receives the argument my ($index, %array_to_count) = @_; # return the count return $#{$array_to_count{$index}}+1; }
    Thoughts,
    -v
    "Perl. There is no substitute."
Re: Answer: How cand I find each dimension x,y,z of a 3D arrays
by ccn (Vicar) on Sep 29, 2004 at 18:08 UTC

    #this prints 3 print getSecondArraySize("la", %test)."\n"; #this prints 3 print scalar @{$test{la}}, "\n";