KAKAK
KAKAK

Reputation: 899

Perl script array and scalar pinpoint

sub main{
my $mark;
my $grade;
my $calc;

@grade = ($mark>=0 and $mark<=39,$mark>=40 and $mark<=49,$mark>=50 and $mark<=59);
@calc(F+,D+,B+);

print "What is the student’s mark?"
chomp($mark = <STDIN>);

print "Your mark is 'mark' and grade is 'calc'"
}
main();

Hi i am a beginner, what i want to do is make different blocks of marks e.g. @mark(0-39,40-49,50-59) will point to the @calc(F+,D+,B+) respectively. After which i can print out the $mark from and also the grade corresponding to the mark. Thank you for your help.

Upvotes: 0

Views: 99

Answers (2)

Andomar
Andomar

Reputation: 238296

You could use an array of grades. Each entry of the array can be a hashtable containing the name of the grade and the minimum and maximum values for that grade:

my @grades = (
    { name => 'F+', min => 0, max => 39 },
    { name => 'D+', min => 40, max => 49 },
    { name => 'B+', min => 50, max => 59 }
    );

print "What is the student’s mark?\n";
chomp(my $mark = <STDIN>);

my $calc = "Unknown";
foreach my $grade (@grades) {
    if ($grade->{min} <= $mark && $mark <= $grade->{max}) {
        $calc = $grade->{name};
    }
}

print "Your mark is '$mark' and grade is '$calc'\n";

Upvotes: 1

ikegami
ikegami

Reputation: 386676

First of all, always use use strict; use warnings;.

Starting with the best letter, find the first letter whose range start is less than the the mark.

my @letters      = qw( F+ D+ B+ );
my @letter_marks =   (  0,40,50);

sub get_letter {
   my ($mark) = @_;
   for my $i (reverse 0 .. $#letters_marks) {
       return $letters[$i] if $mark >= $letter_marks[$i];
   }
   die "Invalid input";
}

Upvotes: 1

Related Questions