Perl 计算以“class”开头的任何文件的平均成绩
我正在为作业编写一个 perl 脚本,该脚本接收以“class”开头的任何文件,并打印出班级标题和文件中每个学生的平均分。
输入:
$cat class1
Sam 90, 95, 82
Max 88, 90, 86
Alex 85, 92, 90
$cat class2
Student1 87, 91, 86
Student2 84, 89, 88
Student3 84, 90, 93
输出:
*****class1*****
Sam: 89
Max: 88
Alex: 89
*****class2*****
Student1: 88
Student2: 87
Student3: 89
脚本的当前尝试:
#!/usr/bin/perl
@gradefiles = <class*>;
foreach (@gradefiles){
print "****".$_."****\n";
my %grades;
my %average;
open(DATA, $_) || die "Can't open $_: $!\n";
while (<DATA>){
my ($student, @vals) = split(/\t/);
$grades{$student} = \@vals;
}
keys %grades;
while(my($k, $v) = each %grades){
my @igrades = split(',',$v);
my $total = 0;
foreach (@igrades){
$total += $_;
}
my $length = @igrades;
my $avg = $total / $length;
$average{$k} = \$avg;
}
keys %average;
while(my($k, $v) = each %average){
print $k." ".$v."\n";
}
}
当前输出:
****class1****
Sam 90, 95, 82
SCALAR(0x2036100)
Alex 85, 92, 90
SCALAR(0x202ab38)
Max 88, 90, 86
SCALAR(0x2034710)
****class2****
Student3 84, 90, 93
SCALAR(0x2034740)
Student2 84, 89, 88
SCALAR(0x2034728)
Student1 87, 91, 86
SCALAR(0x202a298)
=====更新==== 我的新代码
#!/usr/bin/perl
@gradefiles = <class*>;
foreach (@gradefiles){
print "****".$_."****\n";
my %grades;
my %average;
open(DATA, $_) || die "Can't open $_: $!\n";
while (<DATA>){
my ($student, @vals) = split(/\t/);
$grades{$student} = @vals;
}
for my $name (keys %grades){
my @igrades = split(',',$grades{$name});
my $total = 0;
for my $grade (@igrades){
$total += $grade;
}
my $length = @igrades;
my $avg = $total / $length;
$average{$name} = $avg;
}
for my $name (keys %average){
print $name.": ".$average{$name}."\n";
}
}
我的新输出:
****class1****
Sam 90, 95, 82
: 0
Alex 85, 92, 90
: 0
Max 88, 90, 86
: 0
****class2****
Student3 84, 90, 93
: 0
Student2 84, 89, 88
: 0
Student1 87, 91, 86
: 0
工作脚本
#!/usr/bin/perl
@gradefiles = <class*>;
foreach (@gradefiles){
print "****".$_."****\n";
my %grades;
my %average;
open(DATA, $_) || die "Can't open $_: $!\n";
while (<DATA>){
my ($student, $vals) = split(/\t/);
$grades{$student} = $vals;
}
for my $name (keys %grades){
my @igrades = split(',',$grades{$name});
my $total = 0;
for my $grade (@igrades){
$total += $grade;
}
my $length = @igrades;
my $avg = $total / $length;
$average{$name} = $avg;
}
for my $name (keys %average){
print $name.": ".$average{$name}."\n";
}
}
I am writing a perl script for homework that takes in any files that start with "class" and print out the class title and the average of each student in the file.
INPUT:
$cat class1
Sam 90, 95, 82
Max 88, 90, 86
Alex 85, 92, 90
$cat class2
Student1 87, 91, 86
Student2 84, 89, 88
Student3 84, 90, 93
OUTPUT:
*****class1*****
Sam: 89
Max: 88
Alex: 89
*****class2*****
Student1: 88
Student2: 87
Student3: 89
Current Attempt at script:
#!/usr/bin/perl
@gradefiles = <class*>;
foreach (@gradefiles){
print "****".$_."****\n";
my %grades;
my %average;
open(DATA, $_) || die "Can't open $_: $!\n";
while (<DATA>){
my ($student, @vals) = split(/\t/);
$grades{$student} = \@vals;
}
keys %grades;
while(my($k, $v) = each %grades){
my @igrades = split(',',$v);
my $total = 0;
foreach (@igrades){
$total += $_;
}
my $length = @igrades;
my $avg = $total / $length;
$average{$k} = \$avg;
}
keys %average;
while(my($k, $v) = each %average){
print $k." ".$v."\n";
}
}
CURRENT OUTPUT:
****class1****
Sam 90, 95, 82
SCALAR(0x2036100)
Alex 85, 92, 90
SCALAR(0x202ab38)
Max 88, 90, 86
SCALAR(0x2034710)
****class2****
Student3 84, 90, 93
SCALAR(0x2034740)
Student2 84, 89, 88
SCALAR(0x2034728)
Student1 87, 91, 86
SCALAR(0x202a298)
=====UPDATE====
my new code
#!/usr/bin/perl
@gradefiles = <class*>;
foreach (@gradefiles){
print "****".$_."****\n";
my %grades;
my %average;
open(DATA, $_) || die "Can't open $_: $!\n";
while (<DATA>){
my ($student, @vals) = split(/\t/);
$grades{$student} = @vals;
}
for my $name (keys %grades){
my @igrades = split(',',$grades{$name});
my $total = 0;
for my $grade (@igrades){
$total += $grade;
}
my $length = @igrades;
my $avg = $total / $length;
$average{$name} = $avg;
}
for my $name (keys %average){
print $name.": ".$average{$name}."\n";
}
}
My new output:
****class1****
Sam 90, 95, 82
: 0
Alex 85, 92, 90
: 0
Max 88, 90, 86
: 0
****class2****
Student3 84, 90, 93
: 0
Student2 84, 89, 88
: 0
Student1 87, 91, 86
: 0
WORKING SCRIPT
#!/usr/bin/perl
@gradefiles = <class*>;
foreach (@gradefiles){
print "****".$_."****\n";
my %grades;
my %average;
open(DATA, $_) || die "Can't open $_: $!\n";
while (<DATA>){
my ($student, $vals) = split(/\t/);
$grades{$student} = $vals;
}
for my $name (keys %grades){
my @igrades = split(',',$grades{$name});
my $total = 0;
for my $grade (@igrades){
$total += $grade;
}
my $length = @igrades;
my $avg = $total / $length;
$average{$name} = $avg;
}
for my $name (keys %average){
print $name.": ".$average{$name}."\n";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你有点把问题复杂化了。您不需要正在构建的任何哈希值。
I think you're overcomplicating the matter somewhat. You don't need any of the hashes that you're building.
您正在这样做:
存储对标量的引用。因此,当您打印
$average{$k}
时,您会打印出对标量的引用,即SCALAR(0x202a298)
。将行更改为:...一切都应该正常。
You're doing this:
Which stores a reference to the scalar. Thus when you print out
$average{$k}
, you print out the reference to the scalar, which isSCALAR(0x202a298)
. Change the line to:...and everything should work.