Perl 能否将数据从foreach循环(使用内部数组)输出到变量?
我创建了一个等级计算器,它将程序参数(例如90 84 39)转换成字母等级。在我的赋值中,我们必须使用foreach循环和子例程,从@数组调用参数,并将答案连接到名为$output的变量中 我试着用我所知道的来处理很多代码,但我对这方面还是很陌生,不太明白如何将输出连接到变量中Perl 能否将数据从foreach循环(使用内部数组)输出到变量?,perl,Perl,我创建了一个等级计算器,它将程序参数(例如90 84 39)转换成字母等级。在我的赋值中,我们必须使用foreach循环和子例程,从@数组调用参数,并将答案连接到名为$output的变量中 我试着用我所知道的来处理很多代码,但我对这方面还是很陌生,不太明白如何将输出连接到变量中 use strict; use warnings; # This script will use a subroutine to translate all the numbers # entered on the c
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.
使用严格;
使用警告;
#这个脚本将使用一个子例程来翻译所有的数字
#在命令行中输入相应的等级。
#如果程序没有至少一个参数,脚本将结束
如果(@ARGV<1),则“请输入至少一个程序参数以运行此程序”;
#平地机子程序arg将保存在变量$output中
我的$output=&分级器(@ARGV);
#打印行将输出打印为字母等级
打印“您的成绩是:$output\n”;
#初始化子例程分级器
分级机{
我的$output=“”;
#my$currentOutput=shift(@u2;);
#这个foreach循环将程序arg与分数进行比较,并输出一个字母等级
foreach my$currentOutput(@){
#如果分数高于90,学生得A
如果($currentOutput>=90){
$currentOutput=“A”;
$output.=$currentOutput
}
#如果分数高于80,学生得B
elsif($currentOutput>=80){
$currentOutput=“B”;
$output.=$currentOutput
}
#如果分数高于70,学生得C
elsif($currentOutput>=70){
$currentOutput=“C”;
$output.=$currentOutput
}
#如果分数高于60分,学生得D
elsif($currentOutput>=60){
$currentOutput=“D”;
$output.=$currentOutput
}
#如果分数低于60分,学生得F
否则{
$currentOutput=“F”;
$output.=$currentOutput
}
my$currentOutput=shift(@u2;);
}
返回$output;
````}
现在我在打印$output变量时没有得到任何输出。
带行:
$currentOutput = "A ";
$currentOutput = $output . $currentOutput
你说了两件事:
- 请将“A”分配给变量
李>$currentOutput
- 请将串联变量
和$output
分配给变量$currentOutput
李>$currentOutput
$output
中,并使用以下行:
$currentOutput = "A ";
$currentOutput = $output . $currentOutput
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;
}
你说了两件事:
- 请将“A”分配给变量
李>$currentOutput
- 请将串联变量
和$output
分配给变量$currentOutput
李>$currentOutput
$output
使用strict;
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;
}
使用警告;
#这个脚本将使用一个子例程来翻译所有的数字
#在命令行中输入相应的等级。
#如果程序没有至少一个参数,脚本将结束
如果(@ARGV<1),则“请输入至少一个程序参数以运行此程序”;
#平地机子程序arg将保存在变量$output中
我的$output=&分级器(@ARGV);
#打印行将输出打印为字母等级
打印“您的成绩是:$output\n”;
#初始化子例程分级器
分级机{
我的$output=“”;
#my$currentOutput=shift(@u2;);
#这个foreach循环将程序arg与分数进行比较,并输出一个字母等级
foreach my$currentOutput(@){
#如果分数高于90,学生得A
如果($currentOutput>=90){
$currentOutput=“A”;
$output.=$currentOutput
}
#如果分数高于80,学生得B
elsif($currentOutput>=80){
$currentOutput=“B”;
$output.=$currentOutput
}
#如果分数高于70,学生得C
elsif($currentOutput>=70){
$currentOutput=“C”;
$output.=$currentOutput
}
#如果分数高于60分,学生得D
elsif($currentOutput>=60){
$currentOutput=“D”;
$output.=$currentOutput
}
#如果分数低于60分,学生得F
否则{
$currentOutput=“F”;
$output.=$currentOutput
}
#my$currentOutput=shift(@u2;);
}
返回$output;
}
严格使用;
使用警告;
#这个脚本将使用一个子例程来翻译所有的数字
#在命令行中输入相应的等级。
#如果程序没有至少一个参数,脚本将结束
如果(@ARGV<1),则“请输入至少一个程序参数以运行此程序”;
#平地机子程序arg将保存在变量$output中
我的$output=&分级器(@ARGV);
#打印行将输出打印为字母等级
打印“您的成绩是:$output\n”;
#初始化子例程分级器
分级机{
我的$output=“”;
#my$currentOutput=shift(@u2;);
#这个foreach循环将程序arg与分数进行比较,并输出一个字母等级
foreach my$currentOutput(@){
#如果分数高于90,学生得A
如果($currentOutput>=90){
$currentOutput=“A”;
$output.=$currentOutput
}
#如果分数高于80,学生得B
elsif($currentOutput>=80){
$currentOutput=“B”;
$output.=$currentOutput
}
#如果分数高于70,学生得C
elsif($currentOutput>=70){
$currentOutput=“C”;
$output.=$currentOutput
}
#如果分数高于60分,学生得D
elsif($currentOutput>=60){
$currentOutput=“D”;
$output.=$currentOutput
}
#如果分数低于60分,学生得F
否则{
$currentOutput=“F”;
$output.=$currentOutput
}
#my$currentOutput=shift(@u2;);
}
返回$output;
}
我喜欢采用数据驱动的方法解决此类问题。如果我们把分数到等级的映射放在一个散列中,那么它就是