Perl calculate grade averages for any file starting with “class”

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

Answers


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 / @_;
}

Need Your Help

Making dropdown menu stay after clicking

javascript html css drop-down-menu

I'm currently trying to make my menu stay when you click on the menu, i mean so that the dropdown menu stays after clicking on it.

Using XAML ItemTemplates with Metro UI

c# xaml microsoft-metro

The default "split view" template for a Metro application written in C# makes use of an ItemTemplate attribute in the ListView tag. I'm just getting started with XAML and C#, so it's all still a bit

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.