Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Homework help

by ikegami (Patriarch)
on Oct 29, 2004 at 19:23 UTC ( [id://403881]=note: print w/replies, xml ) Need Help??


in reply to Homework help

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$/$/"); }

Replies are listed 'Best First'.
Re^2: Homework help
by Your Mother (Archbishop) on Oct 30, 2004 at 07:04 UTC

    I was playing around with this and noticed a harmless but problematic side-effect of the way you construct the hash.

    %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 join($", sort keys %scores), $/; __END__ 10 2 3 4 5 8 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    Since you're filtering for letters, the numbers don't get picked up, but the maps are having a side-effect mapping of the other maps below them. Parens around the maps seems to fix it.

    I also learned something from your example. I would have predicted that the last unless defined would not work right on empty input b/c $_ still contains a "\n" which would be defined. I would have been wrong.

      I would have predicted that the last unless defined would not work right on empty input b/c $_ still contains a "\n"
      It doesn't work on empty input. It works on eof/error. It's there to prevent the next two lines from giving "undefined value" warnings under use warnings. The last unless length that follows is what detects the empty string (since chomp removed the "\n" by then).
Re^2: Homework help
by !1 (Hermit) on Oct 30, 2004 at 22:16 UTC

    Wow, your %scores isn't defined correctly at all.

    # perl -l 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 "$_ => $scores{$_}" for keys %scores; __END__ S => 1 T => 1 N => 1 K => 1 Y => 1 2 => 1 E => 1 Z => 1 J => 1 W => 1 B => 1 H => 1 D => 1 I => 1 10 => 1 G => 1 U => 1 F => 1 V => 1 Q => 1 M => 1 C => 1 L => 1 A => 1 O => 1 3 => 1 X => 1 P => 1 8 => 1 4 => 1 R => 1 5 => 1

    Perhaps this is a good time for parentheses?

    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 "$_ => $scores{$_}" for keys %scores; __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://403881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found