Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Anonymous Monk
on Sep 28, 2004 at 19:53 UTC ( [id://394736]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found