Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Passing input through a hash then displaying results

by logie17 (Friar)
on Jan 23, 2007 at 07:10 UTC ( #596039=note: print w/replies, xml ) Need Help??


in reply to Passing input through a hash then displaying results

You probably would want to do something like the following:
my $grade = <STDIN>; my %scores = ( "90" => "A", "80" => "B", "70" => "C", "60" => "D", "50" => "F"); my $letter_score = $scores{$grade}; if ($letter_score eq "A"){ #return this }

There is really no need to loop to lookup key/value pairs. I hope this helps. If you're trying to execute statements you may want to look into the eval function.
s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Replies are listed 'Best First'.
Re^2: Passing input through a hash then displaying results
by ikegami (Patriarch) on Jan 23, 2007 at 07:15 UTC
    What if he scored 89? That's why the looping is needed ( if you use a hash ).
      I would think for such a simple program no need to loop, but just simple if control statements would suffice.
      s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

        Sorry, I meant to say "That's why the looping is needed if you use a hash."

        Of course, you could just do

        sub get_score { my ($score) = @_; return 'A' if $score >= 90; return 'B' if $score >= 80; return 'C' if $score >= 70; return 'D' if $score >= 60; return 'F'; }
        How do I represent the values and keys in a loop or conditional statement?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-08-10 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?