Reputation: 11
I created a grade calculator that will take program arguments (ex. 90 84 39) and turn them into letter grades. For my assignment we had to use foreach loops and subroutines, calling the arguments from the @_ array and concatenate the answers into a variable called $output
I've tried to play around with a lot of the code using what I know, but am still very new to this and can't quite figure out how to get my output to concatenate into the variable.
use strict;
use warnings;
# This script will use a subroutine to translate all the numbers
# entered on the command line to a corresponding grade.
#if program does not have at least one arg the script will end
die "Please enter at least one program argument to run this program" if (@ARGV < 1);
#graderator subroutine arg will be saved in variable $output
my $output = &graderator(@ARGV);
#print line will print the output as letter grades
print "Your grades are: $output\n";
#initialize the subroutine graderator
sub graderator {
my $output = "";
# my $currentOutput = shift(@_);
# This foreach loop compares program arg to score, and outputs a letter grade
foreach my $currentOutput (@_) {
# If score is higher than 90, student has an A
if ($currentOutput >= 90) {
$currentOutput = "A ";
$output .= $currentOutput
}
# If score is higher than 80, student has a B
elsif ($currentOutput >= 80) {
$currentOutput = "B ";
$output .= $currentOutput
}
# If score is higher than 70, student has a C
elsif ($currentOutput >= 70) {
$currentOutput = "C ";
$output .= $currentOutput
}
# If score is higher than 60, student has a D
elsif ($currentOutput >= 60) {
$currentOutput = "D ";
$output .= $currentOutput
}
# If score is less than 60, student has a F
else {
$currentOutput = "F ";
$output .= $currentOutput
}
my $currentOutput = shift(@_);
}
return $output;
````}
Right now I don't get any output when printing my $output variable.
Upvotes: 0
Views: 261
Reputation: 69244
I like to take a data-driven approach to problems like this. If we put the score to grade mapping in a hash, then it becomes much easier to change it in the future.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
die "Usage: $0 [list of numeric grades]\n" unless @ARGV;
my @grades = graderator(@ARGV);
say join ', ', @grades;
sub graderator {
# Map scores to grades
my %grades = (
90 => 'A',
80 => 'B',
70 => 'C',
60 => 'D',
0 => 'F',
);
my @grade_letters;
for (@_) {
# Get a reverse-sorted list of the keys in the %grades hash.
for my $score (reverse sort { $a <=> $b } keys %grades) {
# If our score (in $_) is greater than the score in the hash
# then the student has scored that grade.
if ($_ >= $score) {
# Add the grade to the return array.
push @grade_letters, $grades{$score};
# Stop looking for the grade for this score.
last;
}
}
}
return @grade_letters;
}
Upvotes: 1
Reputation: 11
use strict;
use warnings;
# This script will use a subroutine to translate all the numbers
# entered on the command line to a corresponding grade.
# If program does not have at least one arg the script will end
die "Please enter at least one program argument to run this program" if (@ARGV < 1);
# Graderator subroutine arg will be saved in variable $output
my $output = &graderator(@ARGV);
# Print line will print the output as letter grades
print "Your grades are: $output\n";
# Initialize the subroutine graderator
sub graderator {
my $output = "";
# my $currentOutput = shift(@_);
# This foreach loop compares program arg to score, and outputs a letter grade
foreach my $currentOutput (@_) {
# If score is higher than 90, student has an A
if ($currentOutput >= 90) {
$currentOutput = "A ";
$output .= $currentOutput
}
# If score is higher than 80, student has a B
elsif ($currentOutput >= 80) {
$currentOutput = "B ";
$output .= $currentOutput
}
# If score is higher than 70, student has a C
elsif ($currentOutput >= 70) {
$currentOutput = "C ";
$output .= $currentOutput
}
# If score is higher than 60, student has a D
elsif ($currentOutput >= 60) {
$currentOutput = "D ";
$output .= $currentOutput
}
# If score is less than 60, student has a F
else {
$currentOutput = "F ";
$output .= $currentOutput
}
#my $currentOutput = shift(@_);
}
return $output;
}
Upvotes: 0
Reputation: 8376
With lines:
$currentOutput = "A ";
$currentOutput = $output . $currentOutput
you say 2 things:
$currentOutput
;$output
and $currentOutput
to variable $currentOutput
;In your task you say something different: you need to concatenate "A " into $output
Upvotes: 1