Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w-
- use strict;
- use warnings;
- my $INPUTFILE = "matches-2019-2020.txt";
- my %team = ();
- my $line_count = 0;
- open my $f, '<', "$INPUTFILE" or die "Can't open file '$INPUTFILE' : $!\n";
- while (my $line = <$f>) {
- $line_count ++;
- my ($day,$mon,$date,$time,$home_team,$away_team,$home_score,$away_score,$spectator) =
- ($line =~ m|^(\S+)\s+(\d+)/(\d+)\s+(\d+)\.\d+\s+(\S+)\s+-\s+(\S+)\s+(\d+)\s+-\s+(\d+)\s+(\d+)\s*$|);
- # print "$home_team $away_team $home_score $away_score $spectator\n";
- my $home_points = ($home_score > $away_score)?3:
- ($home_score == $away_score)?1:0;
- my $away_points = ($away_score > $home_score)?3:
- ($away_score == $home_score)?1:0;
- $team{$home_team}->{'score_goal'} += $home_score;
- $team{$home_team}->{'concede_goal'} += $away_score;
- $team{$home_team}->{'points'} += $home_points;
- $team{$away_team}->{'score_goal'} += $away_score;
- $team{$away_team}->{'concede_goal'} += $home_score;
- $team{$away_team}->{'points'} += $away_points;
- }
- close $f;
- print "Matches = $line_count\n";
- ## expected result
- # team points score_goal concede_goal
- for my $name (sort {$team{$b}->{'points'} <=> $team{$a}->{'points'}} keys %team) {
- my $points = $team{$name}->{'points'};
- my $score_goal = $team{$name}->{'score_goal'};
- my $concede_goal = $team{$name}->{'concede_goal'};
- printf "%4s %4d %4d %4d\n", $name, $points, $score_goal, $concede_goal;
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement