# assuming our %dictionary = read_dictionary_file_to_hash($dict_file); our %tiles = read_tile_file_to_hash($tile_file); # here we go our $tile_regex = convert_tilehash_to_regex(\%tiles); our %filtered_dictionary; for my $word (keys %dictionary) { my $sorted_word = join('', sort split '', $word); $filtered_dictionary{$word} = 1 if $sorted_word =~ $tile_regex; } sub convert_tilehash_to_regex { my $tile_hashref = shift; my $tile_regex_string = ''; for my $tile ( sort keys %{$tile_hashref} ) { $tile_regex_string .= "$tile{0,$tile_hashref->{$tile}}"; } return qr/^$tile_regex_string$/i; } #### # in init section our $BLANK_TILE = '#'; # in convert_tilehash_to_regex $tile_regex_string .= "$tile{0," . $tile_hashref->{$tile}+$tile_hashref{$BLANK_TILE} . "}";