Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the size of a multidimensional array layer

Status
Not open for further replies.

denis60

Technical User
Apr 19, 2001
89
CA
Hi!
How to know the size of an element of a multidimensional array.
Exemple:
@tst=([1,2][1,2,3][1,2,3,4][1,2,3,4,5]);
I get the size of the array with $#tst
I'm looking for a code for the size of each layer(element)
Thanks in advance
 
How about:
[tt]
$#{$tst[$n]}
[/tt]

where $n is the "layer" number.
 
Or derefernce the element in a scalar context, e.g.,

my @tst=([1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]);
print scalar(@$_), "\n" for @tst;

 
Come to think, $# doesn't return the size of an array, it returns the index of the last element, which is 1 less than the number of elems. E.g.
Code:
#!perl
use strict;
use warnings;

my @tst=([1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]);

print "num\thighest\tarray\n";
print "elems\tindex\telement\n";
for (@tst) {
    print scalar(@$_), "\t", $#$_, "\t[", join(",", @$_), "]\n";
}
Output:
Code:
num     highest array
elems   index   element
2       1       [1,2]
3       2       [1,2,3]
4       3       [1,2,3,4]
5       4       [1,2,3,4,5]
So which you use depends on which you want, the number of elements or the highest index.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top