I found two problems:
You're printing $_, but $r contains what you want to print.
You should uppercase the input (using uc()). Hash lookups are case-sensitive.
I also have two recommendations:
It would make more sense if if ($word eq 'q'){ appeared before the split.
Something's really odd with your foreach+grep. In fact, there's no need for a foreach at all. Get rid of the foreach loops and use just @result = grep {defined $_} @pay{@word}; (which simplifies to @result = grep defined, @pay{@word};). You'd be smart if you went and reread some examples using grep, and make sure you know what @hash{list} returns.
Now you just have to sum up the elements of @result with something like my $sum = 0; $sum += $_ foreach @result;.
You could even remove the grep, replace it with a for/foreach loop that does both the validation and the summing. In fact, that's probably a good idea.
Here's how I'd do it:
my %scores = ( map { $_ => 1 } qw( A E I L N O R S T U ), map { $_ => 2 } qw( B D G ), map { $_ => 3 } qw( C M P ), map { $_ => 4 } qw( F H V W Y ), map { $_ => 5 } qw( K ), map { $_ => 8 } qw( J X ), map { $_ => 10 } qw( Q Z ), ); print("Find the Scrabble value of a word.$/$/"); for (;;) { print("Enter a word (or just Enter to quit): "); local $_ = <STDIN>; last unless defined; chomp; last unless length; $_ = uc($_); my $sum = 0; $sum += $scores{$1} while (/([A-Z])/g); print("Scrabble value: $sum$/$/"); }
In reply to Re: Homework help
by ikegami
in thread Homework help
by gitarwmn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |