chomp (my $grade = ); $grade !~ /^\d+$/ and die "Input must be numeric!\n"; my %scores = ( A => sub { return $_[0] <= 90 ? 1 : 0 }, B => sub { return $_[0] <= 80 ? 1 : 0 }, C => sub { return $_[0] <= 70 ? 1 : 0 }, D => sub { return $_[0] <= 60 ? 1 : 0 }, # just a guess, otherwise like 'B' F => sub { return $_[0] >= 50 ? 1 : 0 }, # really? shouldn't that be '<=' ? ); my $result; foreach my $key (qw(D C B A F)) { if ($scores{$key}->($grade) ) { $result = $key; last; } } print $result,$/;