0
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.

2 Answers 2

1

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";
Sign up to request clarification or add additional context in comments.

5 Comments

I pity he he gets a score of 49.5
thank you so much. It works and very easy to understand the method you have provided.
can you please shed some light on how this bit of code works? $grade->{min}
$grade->{min} looks up the value of the min key in the $grade hashtable
"In the hash referenced by $grade", to be precise. $grade contains a reference, since each element of @grades is a reference to a hash.
1

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";
}

1 Comment

Thank you for answering :), i am still a beginner so the codes look a bit alien to me haha thank you though! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.