use strict; use warnings; if ( @ARGV != 1 ) { die < ) { next if $testword =~ /^[A-Z]/; chomp $testword; next if ( abs( length($word) - length($testword) ) > 3 ); # limit the number of hints by only printing them on some probability if ( rand() < .00025 ) { printf "%s %d\n", $testword, intersect_count( $wordhash, normalize( $testword ) ); } } # takes a word and returns a hash where the keys are the letters in the word # and the values are undef sub normalize { my ( $word ) = @_; $word = lc $word; $word =~ s/[^\w]//g; my %hash = map { $_, undef } split '', $word; return \%hash; } # takes two 'normalize'd hashes and returns the number of common letters sub intersect_count { my ( $h1, $h2 ) = @_; my $count = 0; for ( keys %$h1 ) { $count++ if exists $h2->{ $_ }; } return $count; }