in reply to Methodically search an arbitrary space?
Algorithm::Loops's NestedLoops handles this easily. I added extra arguments to function because function had no way of knowing what each argument was. (Was the first argument a x_pos? a y_pos? etc)
use Algorithm::Loops qw( NestedLoops ); my %params = ( x_pos => [1..10], y_pos => [-5..5], z_pos => [0..15], spin => [0, 1], neato => [1..10], ); my @keys = keys %params; NestedLoops( [ @params{@keys} ], sub { function(map { $keys[$_], $_[$_] } 0..$#keys) } ); sub function { my %args = @_; print(join(', ', map { "$_ => $args{$_}" } keys %args), "\n"); }
Update: You have since indicated you didn't care about the names, so the above simplifies to:
use Algorithm::Loops qw( NestedLoops ); my @params = ( [1..10], [-5..5], [0..15], [0, 1], [1..10], ); NestedLoops(\@params, \&function); sub function { print(join(', ', @_), "\n"); }
I bet you didn't expect the solution to be one line long :)
|
|---|