#!/usr/bin/perl
#
# Join tables
#
use strict;
my $USAGE="USAGE: $0 prelim.table final.table\n";
if (scalar(@ARGV) != 2) {
    die $USAGE;
}

my %teams;
open (PRELIM, $ARGV[0]);
while (<PRELIM>) {
    if ($. == 1) {
	next;
    }
    if (/^Рейтинг/) {
	last;
    }
    chomp;
    my ($num, $score, $total, $rating, @team) = split;
    my $team = join(" ",@team);
    $teams{$team}->{score}=$score;
}
close PRELIM;
open (FINAL, $ARGV[1]);
while (<FINAL>) {
    if ($. == 1) {
	next;
    }
    if (/^Рейтинг/) {
	print;
	last;
    }
    chomp;
    my ($num, $score, $total, $rating, @team) = split;
    
}

foreach $team (sort 
	        {
		    $teams{$b}->{score} <=> $teams{$a}->{score}
		    or
			$teams{$b}->{rating} <=> $teams{$a}->{rating}
		} keys %teams
	       )
{
    printf "%5d ",$teams{$team}->{regnum};
    for ($i=1;$i<=$MAXQUEST;$i++)
    {
	my $answer = $teams{$team}->{answers}[$i];
	my $score = $answers[$i]->{$answer}->{score};
	$score = '-' unless $score;
	if ($SHORT) {
	    printf "%1s",$score;
	} else {
	    printf "%3s", $score;
	}
    }
    printf "%3s",$teams{$team}->{score};
    printf "%4s",$teams{$team}->{rating};
    print " $team";
    print "\n";
}

while (<FINAL>) {
    print;
}
