my %combinations = ( co1 => [ [ qw(one two three four five) ], [ qw(one one two three five) ] ], co2 => [ [ qw(one two three five seven) ], [ qw(one one two three five) ] ], co3 => [ [ qw(one two three five seven) ], [ qw(one two three four five) ] ], co4 => [ [ qw(one two three five seven) ], [ qw(one two three four five) ], [ qw(one one two three five) ] ], ); reorg($_,\%combinations) foreach(keys %combinations); sub reorg{ my($k,$h) = @_; foreach my $combiset(@{$h->{$k}}){ my @uniquesets = grep $_ =~ /\Aus/ keys %combinations; my $num = scalar(@us); foreach my $set ( @$combiset ){ $combinations{'us'.++$num } = $set unless grep @$set {<~~>} @$_, @combinations{@us}; # where {<~~>} is 'these arrays match' # or the front N indices do or ... } }