lundi 22 mars 2021

perl getting 0 instead of output

the file I am working with has the following information in the columns trying to get the name, average, min and max of each name

Name,   Category,    Assignment,  Score,   Possible,
Al,        test,        T1,         90,      100
Ben,       lab,        L1,        80,      100
Al ,       test,        T1,         76,      100
Ben,       lab,         L1,         67,      100

the first issue I had with the code was it kept saying there was an illegal division at line 25, I changed some stuff around and now I'm getting

Name Average Min Max0

as my output instead of anything actually being done math wise I'm not really sure where my issues are

#!/usr/bin/perl
my %total;
my %count;
my %min;
my %max;
while(<>){
    chomp;
    @fields = split(/,/, $_);
    if(@fields[0] !~ /Student/){
        $total{@fields[2]} += @fields[5];
        $count{@fields[5]}++;
    }
    if($min{@fields[2]} > @fields[5]){
        $min{@fields[2]} = @fields[5];
    } elsif($min{@fields}[2] == 0){
        $min{@fields[2]} = @fields[5];
    }
    if($max{@fields[2]} < @fields[5]){
        $max{@fields[2]} = @fields[5]
    }
}
print "Name\tAverage\tMin\tMax";
foreach $total(keys %count){
    print $total . "\t" . $total{$type}/$count{$type} . $min{$type} . "\t" . $min{$type} . "\t". $max{$type} . "\n";
}

the expected output

Name Average   Low High
Q1       83    76    90
L1       73.5  67    80

Aucun commentaire:

Enregistrer un commentaire