in reply to Passing input through a hash then displaying results

Am I close?

So close, yet so far away.

Update: I had kept my solution hidden since it sounded like a homework question. Since others have posted theirs, I'll reveal mine:

use strict; use warnings; my %grade_lookup = ( 90 => "A", 80 => "B", 70 => "C", 60 => "D", ); my $score = <STDIN>; chomp($score); my $grade = "F"; foreach my $gate_score (sort { $a <=> $b } keys %grade_lookup) { if ( $score >= $gate_score ) { $grade = $grade_lookup{$gate_score}; } } print("$score is $grade\n");

But since we want an ordered lookup table, why not use an array?

use strict; use warnings; my @grade_lookup = reverse( [ 90 => "A" ], [ 80 => "B" ], [ 70 => "C" ], [ 60 => "D" ], ); my $score = <STDIN>; chomp($score); my $grade = "F"; foreach (@grade_lookup) { if ( $score >= $_->[0] ) { $grade = $_->[1]; } } print("$score is $grade\n");

Replies are listed 'Best First'.
Re^2: Passing input through a hash then displaying results
by kalaisuresh (Initiate) on Jan 23, 2007 at 08:00 UTC
    #!/usr/bin/perl print "Enter the character\n"; $grade = <STDIN>; chomp($grade); %scores=( 90 => "A", 80 => "B", 70 => "C", 60 => "D", 50 => "F" ); if($grade >= 90) { print $scores{90}; } if($grade < 90 && $grade >= 80 ) { print $scores{80}; } elsif($grade < 80 && $grade >= 70 ) {print "hai"; print $scores{70}; } elsif($grade < 70 && $grade >= 60 ) { print $scores{60}; } elsif($grade < 60 && $grade >= 50 ) { print $scores{50}; }
    try this code