S
Steve (another one)
I have just wasted a crazy amount of time trying to populate an array
containing every combination of n values at y positions. It feels as
though it should be trivial and the sort of thing I do every day, but in
fact I could not find the simple solution that surely must exist.
Below is my best attempt - it will also explain the task if I haven't
been clear - can someone tell me if there is a simpler way of doing it.
Thanks
Steve
(Actually I am probably really hoping you will all say "cool, what a
brilliant way to do it", but I somehow doubt that will happen!)
#!/usr/bin/perl
## find all possible combinations of values at $number_of_position positions
use strict;
my $min_value = 1;
my $max_value = 4;
my $number_of_positions = 3;
my $individual = 0;
my $incrementing = 0;
my $dun;
my @data_set;
my @one_set;
for my $x (0..($number_of_positions-1)){push @one_set,$min_value}
###################################################
sub increment{
if ($one_set[$incrementing] > $max_value){$incrementing++;
$one_set[$incrementing]++;
increment();
$incrementing--;
$one_set[$incrementing]=$min_value;
}
}
###################################################
while (1){
push @{$data_set[$individual]}, @one_set;
# done yet ?
foreach (@one_set){ $dun = ($_ == $max_value); last unless $dun}
last if $dun;
$one_set[$incrementing]++;
$individual++;
increment();
}
# show array
$individual=0;
foreach (@data_set){ print "$individual - @{$_}\n"; $individual++ }
containing every combination of n values at y positions. It feels as
though it should be trivial and the sort of thing I do every day, but in
fact I could not find the simple solution that surely must exist.
Below is my best attempt - it will also explain the task if I haven't
been clear - can someone tell me if there is a simpler way of doing it.
Thanks
Steve
(Actually I am probably really hoping you will all say "cool, what a
brilliant way to do it", but I somehow doubt that will happen!)
#!/usr/bin/perl
## find all possible combinations of values at $number_of_position positions
use strict;
my $min_value = 1;
my $max_value = 4;
my $number_of_positions = 3;
my $individual = 0;
my $incrementing = 0;
my $dun;
my @data_set;
my @one_set;
for my $x (0..($number_of_positions-1)){push @one_set,$min_value}
###################################################
sub increment{
if ($one_set[$incrementing] > $max_value){$incrementing++;
$one_set[$incrementing]++;
increment();
$incrementing--;
$one_set[$incrementing]=$min_value;
}
}
###################################################
while (1){
push @{$data_set[$individual]}, @one_set;
# done yet ?
foreach (@one_set){ $dun = ($_ == $max_value); last unless $dun}
last if $dun;
$one_set[$incrementing]++;
$individual++;
increment();
}
# show array
$individual=0;
foreach (@data_set){ print "$individual - @{$_}\n"; $individual++ }