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

Answer: How cand I find each dimension x,y,z of a 3D arrays

by Anonymous Monk
on Sep 28, 2004 at 19:53 UTC ( #394736=categorized answer: print w/replies, xml ) Need Help??

Q&A > arrays > How cand I find each dimension x,y,z of a 3D arrays - Answer contributed by Anonymous Monk

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";
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-03-03 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (94 votes). Check out past polls.

    Notices?