I have this function that will exclude the redundant permutations. If processes are adjacent and belongs to same group exclude this permutation order. I need to change it like it will check all the processes that belongs to same group and skip all these combination weather they are adjacent or not but belongs to the same group like that :

position: 0, 1,2,3
a ,a,b,a
here all a belongs to same group and b belongs to different group.
this function will exclude the permutation that interchange position 0 (a) to position 1 (a) and i want to modify it like it will exclude all the permutations weather they are adjacent or not like it may exclude position 0(a) to position 3(a) permutation order too . Function is

sub Apply_on_index(&@)
my $func = shift;
my $array = shift;
my $group = shift;
return undef unless (defined $func and defined $array);
my $rest;
my $i;
my $j;
my @array;
my $size = $#{$array}+1;
my $card = factorial($size);
my $res;
@array = @{$array};
$res = [];
$rest = $j;
$i = 0;
for($i = 0; $i <= $#{$array}; $i++){
${$res}[$i] = splice @array, $rest % ($#array + 1), 1;
$rest = int($rest / ($#array + 2));
if ($i > 0 and ${$res}[$i] < ${$res}[$i-1] and
${$group}[${$res}[$i]] == ${$group}[${$res}[$i-1]]){
$res = undef;
&$func($j) if defined $res;
return 0;
Plz let me know how I can modify it?