Do you know where your variables are? PerlMonks

### Comment on

 Need Help??
# Scrabble distribution our %letter_distribution = qw( a 9 b 2 c 2 d 4 e 12 f 2 g 3 h 2 i 9 j 1 k 1 l 4 m 2 n 6 o 8 p 2 q 1 r 6 s 4 t 6 u 4 v 2 w 2 x 1 y 2 z 1 ) ;

You never use this variable anywhere so why is it here?

sort { score_word(\$a) cmp score_word(\$b) }

score_word() returns a numeric value so that should be:

sort { score_word(\$a) <=> score_word(\$b) }

And if you used a Schwartzian Transform you wouldn't have as much overhead on all those subroutine calls.

# search for matching words my @possible_words = map \$_->[ 1 ], sort { \$a->[ 0 ] <=> \$b->[ 0 ] } map length() == length( \$word_pattern ) && pattern_word( \$word_pat +tern, \$_ ) ? [ score_word( \$_ ), \$_ ] : (), @dictionary;

sub score_word { my (\$word) = @_ ; my \$points = 0 ; my @letters = split //, \$word ; \$points += \$letter_points{\$_} foreach @letters ; return \$points ; }

You could use List::Util::sum and reduce that to:

use List::Util qw/ sum /; sub score_word { sum( @letter_points{ split //, \$_[ 0 ] } ) }

my %deny_letters = map { \$_ => 1 } split(//, \$pattern) ; my @p = split //, \$pattern ;

Why split the same thing twice:

my @p = split //, \$pattern ; my %deny_letters = map { \$_ => 1 } @p ;

In reply to Re: Hangman - Hanging with Friends by jwkrahn
in thread Hangman - Hanging with Friends by onelesd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2018-05-20 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (150 votes). Check out past polls.

Notices?