(tye)Re: Length of 2-dimensional array
by tye (Sage) on Apr 06, 2001 at 00:26 UTC
|
$scalar= @array will give you the number of
rows (or columns, depending on your convention) and
$scalar= @{$array[$i]} will give you the
width (or length) of row (or column) number $i.
There is no single width as each row can have a different
width.
-
tye
(but my friends call me "Tye")
| [reply] [d/l] [select] |
Re: Length of 2-dimensional array
by jeroenes (Priest) on Apr 06, 2001 at 00:29 UTC
|
Your question leaves room for interpretation. Move to strike. A fun answer, though certainly not your favorite (or useful?):
my $a; # a 2d-array
my $count=0;
for (@$a){
for (@$_){
$count++;
}
}
print "My 2D-array is $count long\n";
Jeroen
"We are not alone"(FZ) | [reply] [d/l] |
|
This could also be written as:
my $a; # a 2d-array
my $count=0;
for (@$a){
$count += $#$_+1;
}
print "My 2D-array is $count long\n";
| [reply] [d/l] |
|
my $a = [[1,2,3],[4,5,6,7],[3]];
my $count = 0;
$count += @$_ for @$a;
--
$you = new YOU;
honk() if $you->love(perl) | [reply] [d/l] |
|
Yes. And extremely's reply spares even more keystrokes.
Alls that inspires me to give a somewhat more useful answer:
$a = [ [ 0..2 ], [0..10],[0..100] ];
my ($min, $max,$sum) = (undef, 0, 0);
for (@$a){
$sum += my $a_length = @$_;
$min = $a_length if $a_length < $min or not defined $min;
$max = $a_length if $a_length > $max;
}
my $avg = $sum/ @$a;
print "Avg: $avg; Min: $min; Max: $max\n";
Gives some useful array statistics.
Jeroen
"We are not alone"(FZ) | [reply] [d/l] |
Re: Length of 2-dimensional array
by feloniousMonk (Pilgrim) on Apr 06, 2001 at 02:00 UTC
|
--
Oops. I apparently was not clear enough, many apologies.
Here's what I have coming into a sub:
my $data = $_[0];
foreach(@$data){ #These three lines I wanted to replace
$length_of_data++;
}
The actual argument passed to the sub is a 2 dim array
looking like @array[dunno_how_long_this_is][2];
I need to know how many of element dunno_how_long_this_is there are
Make better sense?
The above code works fine, and is efficient enough, I just
like to use elegant Perl constructs when I'm smart enough
to figure them out
--
Felonious | [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
--
OK - that's it. I was looking for the inner length,
thanks.
Ijust wasn't seeing it. I've not yet scrubbed all of this
C from my brain yet so sometimes I get foozeld
Silly me.
--
Felonious
Fooz-eld - definition: Temporarily confused,
frustrated. Prelude to disgruntled
| [reply] |
Re: Length of 2-dimensional array
by satchboost (Scribe) on Apr 05, 2001 at 23:17 UTC
|
Maybe I'm missing something, but wouldn't $#array give you the length of the array? n-dim arrays are really simple arrays, all of whose elements are references to arrays (and so forth for dimensions > 2). Thus, the length is the size of outer-most array. Right? | [reply] |
|
First, $#array is the index of the last element
in the array. This is never the length of the array
itself (unless you've mucked with $[, but don't
do that). The best way to get the length of an array is to
evaluate it in scalar context.
Second, they haven't defined what the "length" of the two
dimensional array is, so it's very difficult to answer the
question. Is "length" the length of the longest sub-array?
The number of elements in the the main array? The total
of all "end level" elements in the array? So maybe your
definition of length is what he needs and maybe it isn't.
Incidently, Tye already illustrated the issues with the
length concepts in another replay: each of the N different
Sub-arrays could have different lengths from each other.
-Ted
| [reply] [d/l] [select] |