total-score 1.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/perl -w

my $SCOREFILE = "score.txt";

open(SCORE,"<$SCOREFILE") or die "Unable to open $SCOREFILE: $!\n";
my @scorefile = <SCORE>;
chomp @scorefile;
close(SCORE);

my $total = 0;
my $total_possible = 0;
Robert Ricci's avatar
Robert Ricci committed
12
my $found_totalline = 0;
13 14

foreach my $line (@scorefile) {
15
    if ($line =~ /^\s*(\-?\d+(\.\d+)?|XX)(\s*\/\s*(\d+)\s*)?/) {
16
        my ($score, $possible) = ($1, $4);
17 18 19 20 21 22 23 24
        if ($score eq "XX") {
            die "Score not filled on line: $line\n";
        } else {
            $total += $score;
        }
        if ($possible) {
            $total_possible += $possible;
        }
25
    } elsif ($line =~ /^\s*Total score:\s*(XX|(\d+(\.\d+)?))?\s*\/\s*(YY|\d+)?/) {
Robert Ricci's avatar
Robert Ricci committed
26
        $found_totalline = 1;
27
        $line = "Total score: $total / $total_possible\n";
28 29 30 31 32 33 34 35 36
        print $line;
    }
}

if ($total_possible == 0) {
    die "Error: Didn't find a non-zero total possible score\n";
}

if (!$found_totalline) {
37
    my $line = "Total score: $total / $total_possible";
38 39 40 41 42 43
    push @scorefile, $line;
    print $line;
}

open(SCORE,">$SCOREFILE") or die "Unable to write to $SCOREFILE: $!\n";

Robert Ricci's avatar
Robert Ricci committed
44
print SCORE map {"$_\n"} @scorefile;
45