You haven't provided an example of the code which runs inside the innermost loop. It might be useful if you provided some idea of what you're trying to do inside of those loops.
I'm assuming that your code only needs to know the loop indices.
Here's how you can use Math::Cartesian::Product:
And here are the results:use Math::Cartesian::Product; # this encodes the number of loops and their ranges my @ranges = ( [ 0..1 ], [ 1..2 ], [2..3] ); # the code inside the braces is run for every combination # of the ranges cartesian { print "indices: @_\n"; } @ranges; # here's a version using a subroutine which sums up # the indices (just to do something with them) sub compute { my $sum = 0; $sum += $_ foreach @_; print "sum of @_ = $sum\n"; } cartesian \&compute, @ranges;
indices: 0 1 2 indices: 0 1 3 indices: 0 2 2 indices: 0 2 3 indices: 1 1 2 indices: 1 1 3 indices: 1 2 2 indices: 1 2 3 sum of 0 1 2 = 3 sum of 0 1 3 = 4 sum of 0 2 2 = 4 sum of 0 2 3 = 5 sum of 1 1 2 = 4 sum of 1 1 3 = 5 sum of 1 2 2 = 5 sum of 1 2 3 = 6
In reply to Re^3: How to create loop in perl dynamically
by djerius
in thread How to create loop in perl dynamically
by adithi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |