Iterating over all elements in a 3D array

  • Thread starter it_says_BALLS_on_your forehead
  • Start date
I

it_says_BALLS_on_your forehead

Is there cleaner way of writing this:

foreach $send (0...$#sr) {
foreach $recv (0...$#{$sr[$send]}) {
foreach $round (0...$#{$sr[$send][$recv]}) { print
"$sr[$send][$recv][$round]:\n";
}
}



}

yes. you needn't use an integer iterator. it's actually more efficient
(and cleaner) to iterate over the array elements themselves.

for my $lev1 ( @sr ) {
for my $lev2 ( @{$lev1} ) {
for my $lev3 ( @{$lev2} ) {
print "$lev3\n";
}
}
}

....you can also probably use maps or something more esoteric if you're
more concerned with brevity and less concerned with readability. also,
i'm not sure if it's more efficient to dereference the arrayref
outside the nested loops or not. if i weren't so lazy i'd benchmark...
 
I

it_says_BALLS_on_your forehead

nikolas said:
Is there cleaner way of writing this:
foreach $send (0...$#sr) {
foreach $recv (0...$#{$sr[$send]}) {
foreach $round (0...$#{$sr[$send][$recv]})
{ print "$sr[$send][$recv][$round]:\n";
}
}
}

Actually sorry what I mean is a more generic way of doing this one which
would work with an N dimensional array.

yes. you can write a recursive function, and even imbue it with
intelligence enough to handle nested hashrefs as well as arrayrefs via
the ref function.
 
A

anno4000

nikolas pontikos said:
nikolas said:
Is there cleaner way of writing this:

foreach $send (0...$#sr) {
foreach $recv (0...$#{$sr[$send]}) {
foreach $round (0...$#{$sr[$send][$recv]})
{ print "$sr[$send][$recv][$round]:\n";
}
}
}

Actually sorry what I mean is a more generic way of doing this one which
would work with an N dimensional array.

Sure.

sub flatten { map ref() ? flatten( @$_) : $_, @_ }

With that you can

print "$_\n" for flatten(
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
);

The arrays can be arbitrarily nested:

print "$_\n" for flatten(
[ 1, [ 2, 3, 4], [ 4, [ 5, 6], [[[[ 7]]]] ] ], 8, 9,
);

Anno
 
N

nikolas pontikos

Is there cleaner way of writing this:

foreach $send (0...$#sr) {
foreach $recv (0...$#{$sr[$send]}) {
foreach $round (0...$#{$sr[$send][$recv]}) { print
"$sr[$send][$recv][$round]:\n";
}
}
}
 
N

nikolas pontikos

nikolas said:
Is there cleaner way of writing this:

foreach $send (0...$#sr) {
foreach $recv (0...$#{$sr[$send]}) {
foreach $round (0...$#{$sr[$send][$recv]})
{ print "$sr[$send][$recv][$round]:\n";
}
}
}

Actually sorry what I mean is a more generic way of doing this one which
would work with an N dimensional array.
 
N

nikolas pontikos

Michele said:
Is there cleaner way of writing this:

foreach $send (0...$#sr) {
foreach $recv (0...$#{$sr[$send]}) {
foreach $round (0...$#{$sr[$send][$recv]}) { print
"$sr[$send][$recv][$round]:\n";
}
}
}

local $\=":\n";
for (@sr) {
for(@$_) {
for (@$_) {
print;
}
}
}


Michele

So $\ is a perl variable that's appended to every string which gets
printed out? That's pretty cool I didn't know that.

Thanks,
 

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
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top