G
Gunnar Hjalmarsson
Jack said:Wondering if anyone knows the best way to count the number of rows
in a multidimensional array.
What's a row?
Jack said:Wondering if anyone knows the best way to count the number of rows
in a multidimensional array.
Hello,
Wondering if anyone knows the best way to count the number of rows in
a multidimensional array.
Gunnar Hjalmarsson said:What's a row?
Jack said:Ok, what is the best way to count the number of *arrays* in a
multidimensional array.
Paul Lalli said:Hello,
Wondering if anyone knows the best way to count the number of rows in
a multidimensional array.
In the first place, how are you defining a 'row'? In your own personal
visualization of a 2d array, do you see the 'rows' as the "inner" arrays,
or the "outer" array? In other words, given:
my @foo = (1..5);
my @bar = ('a'..'j');
my @two_d = (\@foo, \@bar);
what are the 'rows'? @foo and @bar? or @two_d?
In either case, to answer your question, you should never 'count' how many
elements a perl array has. You should just have perl tell you straight
out. Evaluating an array in scalar context gives the size of that array:
print "Size of numbers: " . @{$two_d[0]} . "\n";
print "Size of letters: " . @{$two_d[1]} . "\n";
print "Size of outer: " . @two_d . "\n";
Paul Lalli
Here is what I am after, here is my 2Darray sorry if it has not been
clear:
['0', '1', '2'],
['3', '4', '5'],
['6', '7', '8']
How do I ask perl to tell me I have 3 horizontal arrays (rows), well 2
if you count 0.... ???
Here is what I am after, here is my 2Darray sorry if it has not been
clear:
['0', '1', '2'],
['3', '4', '5'],
['6', '7', '8']
How do I ask perl to tell me I have 3 horizontal arrays (rows), well 2
if you count 0.... ???
Paul Lalli said:Hello,
Wondering if anyone knows the best way to count the number of rows in
a multidimensional array.
In the first place, how are you defining a 'row'? In your own personal
visualization of a 2d array, do you see the 'rows' as the "inner" arrays,
or the "outer" array? In other words, given:
my @foo = (1..5);
my @bar = ('a'..'j');
my @two_d = (\@foo, \@bar);
what are the 'rows'? @foo and @bar? or @two_d?
In either case, to answer your question, you should never 'count' how many
elements a perl array has. You should just have perl tell you straight
out. Evaluating an array in scalar context gives the size of that array:
print "Size of numbers: " . @{$two_d[0]} . "\n";
print "Size of letters: " . @{$two_d[1]} . "\n";
print "Size of outer: " . @two_d . "\n";
Paul Lalli
Here is what I am after, here is my 2Darray sorry if it has not been
clear:
['0', '1', '2'],
['3', '4', '5'],
['6', '7', '8']
How do I ask perl to tell me I have 3 horizontal arrays (rows), well 2
if you count 0.... ???
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.