while () { chomp; printf "%4.2f $_\n", score (\%rows, \%cols,$_) if length >=$MIN && not /[A-Z]/; } #### unless length < $MIN || /[A-Z]/; #### sub score { my ($rows, $cols, $str) = @_; my ($tot, $ch); my ($r, $c, $lr, $lc); my ($leastc,$mostc); ($ch) = $str =~ /(.)/; ($lr, $lc,$leastc,$mostc) = ($rows->{$ch}, ($cols->{$ch}) x 3); for $ch (split('', $str)) { ($r, $c) = ($rows->{$ch}, $cols->{$ch}); $tot += abs($lr - $r) + abs($lc - $c); $leastc = $c if $c < $leastc; $mostc = $c if $c > $mostc; ($lr, $lc) = ($r, $c); } return ($tot / (length($str) - 1) + ($mostc-$leastc) / 3); }