best way to count the number of rows in a multidimensional array

  • Thread starter Gunnar Hjalmarsson
  • Start date
J

Jack

Hello,

Wondering if anyone knows the best way to count the number of rows in
a multidimensional array.

Thanks,

Jack
 
P

Paul Lalli

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
 
G

Gunnar Hjalmarsson

Jack said:
Ok, what is the best way to count the number of *arrays* in a
multidimensional array.

Aha, so that's what you meant.

I suppose you can have a counter incremented while looping through the
AoA. "perldoc perldsc" or "perldoc perllol" should give you some
guidance how to dereference in order to reach the next level.

What's the overall structure of the AoA you want to explore? What have
you tried so far?
 
J

Jack

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.... ???
 
T

Tore Aursand

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.... ???

my @array = (
['0', '1', '2'],
['3', '4', '5'],
['6', '7', '8']
);
print 'Number of elements: ' . @array . "\n";
 
A

Anno Siegel

[...]
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.... ???

"2 if you count 0"? What is that supposed to mean? That the highest
index is 2?

Your question could indeed have been clearer. The answer is trivial.
Each line ("row" is ambiguous) is an arrayref stored in your 2-d array.
So the number of lines in the array is just the number of elements.
Use it in scalar context:

my $n_of_lines = @array;

Anno
 
P

Paul Lalli

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.... ???

Out of curiousity, did you actually try the code I listed above? If you
had, you would have seen this output:
Size of numbers: 5
Size of letters: 10
Size of outer: 2

Hopefully you recognized that the code I used generated this structure:

@two_d = (
[1, 2, 3, 4, 5],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
);

So in your terms, there are two rows. In a more Perl way of speaking,
@two_d contains two array references. Since this is the number you're
trying to get, you just take the size of @two_d, as I demonstrated above:

print "Size of outer: " . @two_d . "\n";


Is there anything about this you don't understand? Is there something I
can be more clear on?

Paul Lalli
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top