my %found; for ( 1 .. $length ) { my $P = Algorithm::Permute->new( \@input_chars, $_ ); while ( my @res = $P->next ) { my $word = join '', @res; $found{ $word } = calc_score( $word ) if exists $words{ $word }; } }