Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need 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$/$/"); }

In reply to Re: Homework help by ikegami
in thread Homework help by gitarwmn

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2023-09-28 18:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found