此解决方案假定参数排序无关紧要,只要涵盖所有情况。如果对您的问题很重要,我认为 CPAN 有一些有序的散列。
use strict;
my %KNOBS = (ARG1=>[1,2,3],
ARG2=>['a','b'],
ARG3=>['41:R']);
my %indicies;
foreach my $keys (keys %KNOBS)
{
$indicies{$keys}=0;
}
my @orderedkeys = (keys %KNOBS);
printknobs();
while(incrimentindicies())
{
printknobs();
}
sub printknobs
{
foreach (@orderedkeys)
{
print "-$_ $KNOBS{$_}[$indicies{$_}] "; #num elements in array $key of %knob
}
print "\n";
}
sub incrimentindicies
{
foreach (@orderedkeys)
{
if( $indicies{$_} + 1 < @{$KNOBS{$_}})
{
$indicies{$_} = $indicies{$_} + 1;
return 1;
}else{
$indicies{$_} = 0;
}
}
return 0;
}
输出:
-ARG2 a -ARG3 41:R -ARG1 1
-ARG2 b -ARG3 41:R -ARG1 1
-ARG2 a -ARG3 41:R -ARG1 2
-ARG2 b -ARG3 41:R -ARG1 2
-ARG2 a -ARG3 41:R -ARG1 3
-ARG2 b -ARG3 41:R -ARG1 3
@orderedkeys
确保%indicies
在调用incrimentindicies
.