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 |