XP is just a number PerlMonks

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

by DrHyde (Prior)
 on Jun 19, 2003 at 09:33 UTC Need Help??

Q&A > arrays > How cand I find each dimension x,y,z of a 3D arrays - Answer 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]);
}

Create A New User
Chatterbox?
 [moritz]: I had an office move too, two floors up [moritz]: so far I've managed not to get into my old office by accident, but once it was close (already stopped at the wrong floor)

How do I use this? | Other CB clients
Other Users?
As of 2017-10-17 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My fridge is mostly full of:

Results (224 votes). Check out past polls.

Notices?