Perl 计算以“class”开头的任何文件的平均成绩

发布于 2024-12-19 05:53:52 字数 3159 浏览 0 评论 0原文

我正在为作业编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2024-12-26 05:53:52

我认为你有点把问题复杂化了。您不需要正在构建的任何哈希值。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010; # For 'say'

foreach my $class (<class*>) {
  say "****$class****";

  # Three-arg open, lexical filehandle. Modern Perl best practice    
  open my $data, '<', $class or die "Can't open $class: $!\n";

  while (<$data>) {
    my ($student, @data) = split /[\s,]+/;
    say "$student: ", avg(@data);
  }
}

sub avg {
  my $total;
  $total += $_ for @_;

  return $total / @_;
}

I think you're overcomplicating the matter somewhat. You don't need any of the hashes that you're building.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010; # For 'say'

foreach my $class (<class*>) {
  say "****$class****";

  # Three-arg open, lexical filehandle. Modern Perl best practice    
  open my $data, '<', $class or die "Can't open $class: $!\n";

  while (<$data>) {
    my ($student, @data) = split /[\s,]+/;
    say "$student: ", avg(@data);
  }
}

sub avg {
  my $total;
  $total += $_ for @_;

  return $total / @_;
}
安静 2024-12-26 05:53:52

您正在这样做:

$average{$k} = \$avg;

存储对标量的引用。因此,当您打印 $average{$k} 时,您会打印出对标量的引用,即 SCALAR(0x202a298)。将行更改为:

$average{$k} = $avg;

...一切都应该正常。

You're doing this:

$average{$k} = \$avg;

Which stores a reference to the scalar. Thus when you print out $average{$k}, you print out the reference to the scalar, which is SCALAR(0x202a298). Change the line to:

$average{$k} = $avg;

...and everything should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文