in reply to Combinations of a set of elements
use strict; my @chars = ("a", "a", "b", "c"); my %non_repeat; foreach (@chars) { if (exists($non_repeat{$_})) { $non_repeat{$_} = 2; } else { $non_repeat{$_} = 1; } } my @combinations; my ($first, $second); my @keys = keys %non_repeat; my ($index1, $index2); for ($index1 = 0; $index1 <= $#keys; $index1 ++) { if ($non_repeat{$keys[$index1]} == 2) { push @combinations, $keys[$index1] x 2; } for ($index2 = $index1 + 1; $index2 <= $#keys; $index2 ++) { push @combinations, $keys[$index1] . $keys[$index2]; push @combinations, $keys[$index2] . $keys[$index1]; } } print join(",", @combinations);
|
|---|