my (%lookup, @mapping); CHAR: for my $char (split //, $sh_str) { my @chr_pos_all_strs; for (0 .. $#pos) { # Ignore chars not in every string next CHAR if ! $pos[$_]{$char}; # All slots to be variable number of positions push @chr_pos_all_strs, $pos[$_]{$char}; } my $next = NestedLoops([@chr_pos_all_strs]); # Generate all combinations while (my @char_map = $next->()) { # stringified reference to convert back to char later my $ref = [@char_map]; $lookup{$ref} = $char; push @mapping, $ref; } }