Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

( #70962=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie Arnaud on Apr 09, 2001 at 13:54 UTC
Q&A  > arrays


Description:

I try to find the size of each dimension (sizeX, sizeY, sizeZ) of my 3D array
which looks like the following:
$myArray["service1"][0][0]="100"; $myArray["service1"][0][1]="101"; $myArray["service2"][1][2]="212"; ...

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

An important point that nobody has pointed out:

$myArray["service1"]
will not do what you want. If you want to look up things by name, you will need a hash.

For learning and playing around with data structures like this I recommend using Data::Dumper so you can easily see what Perl really has done (which may be very different from what you think it is doing).

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

This code will recurse through the elements, and print the number of array-elements for each array in your structure. Will not decent hashes but can be hacked a bit to do that as well.

use strict; my @a = ([([0..3] ) x 4]) x 3; arraylength( \@a ); sub arraylength{ my $a = shift; my $dim = 0; _length( $a, $dim ); } sub _length{ my $a = shift; my $dim = shift; if ( ref( $a ) =~ /ARRAY/ ){ print "\t" x $dim; print "dim $dim -> ".(scalar @$a)." elements"; print "\n\n" if $dim == 0; print "\n" if $dim == 1; _length( $_, $dim + 1) for ( @$a ); print "\n"; } }
This example prints:
dim 0 -> 3 elements dim 1 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 1 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 1 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements dim 2 -> 4 elements
Answer: How cand I find each dimension x,y,z of a 3D arrays
contributed by DrHyde

In a perl array of arrays, just like in C, the data structure does not have to be rectangular (or cuboidal, or ...). For example, you can have an array like so:

@foo = ( [0, 1, 2, 3, 4, 5], [0, 1, 2], [0, [ 0, 1, 2, 3 ]] );
There, @foo contains three references to arrays, all of which are of different lengths. One of them contains a reference to another array as well as an ordinary scalar. The following code will tell you the largest number of elements in each dimension. Dimensions are numbered starting at zero (I assume $[ has not been altered):
my @foo = ( [0, 1, 2, 3, 4, 5], [0, 1, 2], [0, [ 0, 1, 2, 3 ]] ); my @maxlength = (); figger_out_depth(0, @foo); print "max dimensions:\n"; print " $_: $maxlength[$_]\n" foreach (0..$#maxlength); sub figger_out_depth { my $depth = shift; $maxlength[$depth] = 0 unless(defined($maxlength[$depth])); foreach my $element (@_) { figger_out_depth($depth + 1, @{$element}) if(ref($element) eq +'ARRAY'); } $maxlength[$depth] = $#_ + 1 if($#_ >= '0'.$maxlength[$depth]); }
Answer: How cand I find each dimension x,y,z of a 3D arrays
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; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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: (11)
    As of 2014-08-23 04:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (172 votes), past polls