# FILE fhscocomb.pl  working copy  running time 17 sec

#============= CREATE SCORES PAGE ===================
$scofile='FHsco01.txt'; 
open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
open (SCORESHTML, ">fhscores01.html");
print SCORESHTML "<html><head><title>CIAU Field Hockey scores 2001</title>
</head>
<body>
<Font Face=Arial size=-1>\n
<big><b>FH Scores 2001</b></big>";

open (SCORESHTML, ">>fhscores01.html");
while (<SCORES>) { 
	($date,$Tm1,$Sco1,$Tm2,$Sco2,$GmType,$Notes) = split(':',$_);	

  ### Flag any reversed score, stops program, prints line to warn 
if ($Sco1 < $Sco2) { 
die
print "$date $Tm1 $Sco1 $Tm2 $Sco2 $GmType $Notes";
}

                         
if  ($Tm1 le " ")  {              # print date if alone on datafile row
  print SCORESHTML "<br><b>$date</b>";
}              
#elsif ($date =~ /..../)  {
#  print SCORESHTML "<br>$date";
#}
elsif ($Tm1 gt " ")  {                #print score lines without dates    
  print SCORESHTML "<br>$Tm1 $Sco1 $Tm2 $Sco2 $GmType $Notes";
}
                          
}                       
print SCORESHTML "</body></html>"; #close HTML file
close SCORESHTML;

#====================================================
#### TEAM-BY-TEAM SCORES ###
#====================================================

$scofile='FHsco01.txt'; 
open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
open (TEAMDATA, ">fhteams01.html");
print TEAMDATA "
<HTML><HEAD>

<STYLE>
TD {font: 10pt arial}
</STYLE>
</head>
<BODY bgcolor=white LINK=\"\#0000ff\" VLINK=\"\#0000ff\"> <Font Face=Arial Size=-1>

<TABLE border=0  cellspacing=4 cellpadding=4>
<TR>
<TD  colspan=4 align=center  BGCOLOR=\"\#d2d5e1\">
<B>2001 FH SCORES by Team</B><small>

<TR>
<TD  NOWRAP align=center valign=top BGCOLOR=\"\#d2d5e1\">
<B>AUS</B><Br>
<A HREF=\"\#Saint Mary's\">Saint Mary's</A><Br>
<A HREF=\"\#UNB\">UNB</A><Br>
<A HREF=\"\#UPEI\">UPEI</A><Br>

<TD  align=center valign=top BGCOLOR=\"\#d2d5e1\">
<B>OUA</B><Br>
<A HREF=\"\#Carleton\">Carleton</A><Br>
<A HREF=\"\#McGill\">McGill</A><Br>
<A HREF=\"\#Queen's\">Queen's</A><Br>
<A HREF=\"\#Trent\">Trent</A><Br>
<A HREF=\"\#Guelph\">Guelph</A><Br>
<A HREF=\"\#Toronto\">Toronto</A><Br>
<A HREF=\"\#Waterloo\">Waterloo</A><Br>
<A HREF=\"\#Western\">Western</A><Br>
<A HREF=\"\#York\">York</A><Br>


<TD  align=center valign=top BGCOLOR=\"\#d2d5e1\">
<B>CanWest</B><Br>
<A HREF=\"\#Alberta\">Alberta</A><Br>
<A HREF=\"\#Calgary\">Calgary</A><Br>
<A HREF=\"\#Manitoba\">Manitoba</A><Br>
<A HREF=\"\#Victoria\">Victoria</A><Br>
<A HREF=\"\#UBC\">UBC</A><Br>
</TR></TD></TABLE>
&nbsp;
";

@TeamName = ("Saint Mary's", "UNB", "UPEI",
"Queen's", "Carleton", "McGill", 
"Toronto", "Trent", "York", 
"Guelph", "Waterloo", "Western",  
"Alberta", "Calgary", "Manitoba", "Victoria", "UBC",);


foreach $team (@TeamName) {
open (TEAMDATA, ">>fhteams01.html");

print TEAMDATA "<p><dd><big><big><A NAME=\"$team\">$team</A></big></big>\n";

open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
while (<SCORES>) { 
	($Date,$Tm1,$Sco1,$Tm2,$Sco2,$GmType,$Notes) = split(':',$_);	
   
if  ($Tm1 eq $team || $Tm2 eq $team)   {   
  print TEAMDATA "<br>$Date $Tm1 $Sco1 $Tm2 $Sco2 $GmType $Notes\n";
     }
}
}
print TEAMDATA "</body></html>";


#====================================================
#### CREATING STANDINGS MDA file ###
#====================================================


$scofile='FHsco01.txt'; 
 open (FHMDA, ">fhstand.txt") || die "Can't open fhstand.txt $!"; 
print FHMDA "";

@TeamName = ("Saint Mary's", "UNB", "UPEI",
"Queen's", "Carleton", "McGill", 
"Toronto", "Trent", "York", 
"Guelph", "Waterloo", "Western",  
"Alberta", "Calgary", "Manitoba", "Victoria", "UBC",);


foreach $team (@TeamName) {
  open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
  open (FHMDA, ">>fhstand.txt") || die "Can't open fhstand.txt $!"; 

($teamwins, $teamlosses, $pfor, $Against, $WLpct) =0;  # initialize counts
$teamlosses=0; $teamgp=0; $teamties=0; $pfor=0; $Against=0;
$WLpct=0; $Margin=0; $Pts=0; $WLpct=0; $pfpg=0; $papg=0; $mgp=0;  
              # while reading SCORES (FHsco01.txt) line-by-line 
              # parse it at colons : into standings variables  
while (<SCORES>) {    
	($date,$Tm1,$Sco1,$Tm2,$Sco2,$GmType,$Notes) = split(':',$_);	


                   #-------------------------------------------
                   # BEGIN calculation of standings variables.
                   #-------------------------------------------
if ($Tm1 eq $team and $GmType eq "" and $Notes !~ /TIE/ )   { 
                                     # selects Conference only
++$teamgp; ++$teamwins ;             # counts name in winner column
$pfor = ($pfor + $Sco1);             # adds winner's score to FOR 
$Against = ($Against + $Sco2);       # adds loser's score to AGAINST
}
elsif ($Tm2 eq $team && $GmType eq "" && $Notes !~ /TIE/ )   {
++$teamgp; ++$teamlosses ;           # counts name in loser column 
$pfor = ($pfor + $Sco2);             # adds loser's score to FOR
$Against = ($Against + $Sco1);       # adds winner's score to AGAINST 
}
elsif (($Tm2 eq $team || $Tm1 eq $team) && $GmType eq "" && $Notes =~ "TIE" )   {
++$teamgp; ++$teamties; 
$pfor = ($pfor + $Sco1);             # adds tie score to FOR
$Against = ($Against + $Sco1);       # adds tie score to AGAINST 
}

$Margin = ($pfor - $Against);        # Calculates +/- diff

 
if ($team =~ /Carleton|McGill|Queen's|Toronto|Trent|York|Guelph|Waterloo|Western/) {
$Pts = (3 * $teamwins) + $teamties;
}
elsif ($team =~ /Saint Mary's|UNB|UPEI|Alberta|Calgary|Manitoba||UBC|Victoria/) {
$Pts = (2 * $teamwins) + $teamties;
}

if ($teamgp == 0) {  #NEW
    $WLpct = 1.000 ; #NEW
}                    #NEW
                                     # calc W/L %, elim div by zero 
elsif ($teamlosses == 0 and $teamties == 0 and $teamgp != 0) {  
$WLpct = 1.000;                          #NEW > > > > > > >
}
else {
$WLpct = ( ($teamwins + ($teamties * .5 )) / $teamgp );
}
if ($teamgp != 0) {
$pfpg = ($pfor / $teamgp);
$papg = ($Against / $teamgp);
$mgp = ($Margin / $teamgp);
}

if ($team =~ /Saint Mary's|UNB|UPEI/) {   
   $conf = "aAU";
   }
elsif ($team =~ /Carleton|McGill|Queen's/) {
   $conf = "bOUE";
   }
elsif ($team =~ /Toronto|Trent|York/) {
   $conf = "cOUC";
   }
elsif ($team =~ /Guelph|Waterloo|Western/) {
   $conf = "dOUW";
   }
elsif ($team =~ /Alberta|Calgary|Manitoba||UBC|Victoria/) {
   $conf = "eCW";
   }
}   
 
            # END calculation of standings variables for Team.

print FHMDA "$team:$conf:$teamgp:$teamwins:$teamlosses:$teamties:$pfor:$Against:$Margin:$Pts:$WLpct:$pfpg:$papg:$mgp\n";
}
close FHMDA;

#====================================================
#### SORTING STANDINGS ###
#====================================================

$stanfile='fhstand.txt'; 
open (FHMDA, "<fhstand.txt") || die "Can't open $stanfile $!"; 

@mda = <FHMDA>; 
@mdasorted = map { $_->[0] }
 sort {
       $a->[2] cmp $b->[2]
||    $b->[10] <=> $a->[10]
||    $b->[11] <=> $a->[11]
||    $b->[14] <=> $a->[14]
||     $a->[1] cmp $b->[1] 
}
map { [ $_, (split /:/, $_,) ] } 
@mda; 

open (STANHTML, ">fhstand01.html");
print STANHTM "";
open (STANHTML, ">>fhstand01.html");

open (STANAUS, ">fhstanaus.txt");
open (STANOUE, ">fhstanoue.txt");
open (STANOUC, ">fhstanouc.txt");
open (STANOUW, ">fhstanouw.txt");
open (STANCW, ">fhstancw.txt");

print STANAUS @mdasorted[0 .. 2];  ##AUS .. begins zero
print STANOUE @mdasorted[3 .. 5];  ##OUAE
print STANOUC @mdasorted[6 .. 8];  ##OUAC
print STANOUW @mdasorted[9 .. 11];  ##OUAW
print STANCW @mdasorted[12 .. 16];  ##CanWest

##======================================================
print STANHTML "
<HTML><HEAD><title>FH Standings 2001</title>
<STYLE>
td {font: 10pt arial; text-align:right}
th {font: 10pt arial; font-weight=bold; text-align:right}
</STYLE>
</head>
<BODY bgcolor=white LINK=0000ff VLINK=0000ff> <Font Face=Arial Size=-1>
<TABLE border=0  cellspacing=3 cellpadding=0 bgcolor=white>
<TR>
<TD style=text-align:center colspan=8>
<B>2001 Field Hockey Standings</B></td></TR>";
##======================================================
##================AUS=======================
print STANHTML "
<tr><th style=text-align:left> Atlantic </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANAUS, "<fhstanaus.txt");

while (<STANAUS>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$tmpct = sprintf("%.3f", $tmpct);   ## converts to 3 places
$tmpct =~ s/^0/ /g;                 ## replace Leading Zeros

print STANHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $tmpct<td></tr>";
}
##================OUE=======================

print STANHTML "
<tr><th style=text-align:left> OUA East </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANOUE, "<fhstanoue.txt");

while (<STANOUE>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$tmpct = sprintf("%.3f", $tmpct);   ## converts to 3 places
$tmpct =~ s/^0/ /g;                 ## replace Leading Zeros

print STANHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $tmpct<td></tr>";
}

##================OUC=======================

print STANHTML "
<tr><th style=text-align:left> OUA Central</th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANOUC, "<fhstanouc.txt");

while (<STANOUC>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$tmpct = sprintf("%.3f", $tmpct);   ## converts to 3 places
$tmpct =~ s/^0/ /g;                 ## replace Leading Zeros

print STANHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $tmpct<td></tr>";
}


##================OUW=======================

print STANHTML "
<tr><th style=text-align:left> OUA West</th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANOUW, "<fhstanouw.txt");

while (<STANOUW>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$tmpct = sprintf("%.3f", $tmpct);   ## converts to 3 places
$tmpct =~ s/^0/ /g;                 ## replace Leading Zeros

print STANHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $tmpct<td></tr>";
}

##================CW=======================

print STANHTML "
<tr><th style=text-align:left> CanWest </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANCW, "<fhstancw.txt");

while (<STANCW>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$tmpct = sprintf("%.3f", $tmpct);   ## converts to 3 places
$tmpct =~ s/^0/ /g;                 ## replace Leading Zeros

print STANHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $tmpct<td></tr>";
}

print STANHTML "</td></tr></table>";
    ### END STANDINGS PAGE ###

##  Tm conf g w l t  f  a    m  p    %    fgp   agp     mgp  
##York:aAUS:8:3:5:0:99:205:-106:6:0.375:12.375:25.625:-13.25
##  1    2  3 4 5 6  7  8    9  10   11    12     13     14

#------------------------------------------------------------------
#------------------------------------------------------------------
    ### PREPARE HIGH SCORING PAGE ###
open (HIGHHTML, ">fhhigh01.html") || die "Can't open $stanfile $!"; 
print HIGHHTML "";
print HIGHHTML "
<HTML><HEAD><title>FH Highest Scoring 2001</title>
<STYLE>
td {font: 10pt arial; text-align:right}
th {font: 10pt arial; font-weight=bold; text-align:right}
</STYLE>
</head>
<BODY bgcolor=white LINK=0000ff VLINK=0000ff> <Font Face=Arial Size=-1>
<TABLE border=0  cellspacing=3 cellpadding=0 bgcolor=white>
<TR>
<TD align=center colspan=8>
<B>2001 Team Comparisons </B>sorted by:
</td></tr>

<tr><th style=text-align:left>   High Scoring   </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Fpg <th></tr>";


open (FHMDA, "<fhstand.txt") || die "Can't open $stanfile $!";
@mda = <FHMDA>; 
@mdasorted = map { $_->[0] }
 sort {
      $b->[12] <=> $a->[12]   
||    $b->[10] <=> $a->[10]
||    $b->[11] <=> $a->[11]
||    $b->[14] <=> $a->[14]
||     $a->[1] cmp $b->[1] 
}
map { [ $_, (split /:/, $_,) ] } 
@mda; 


open (FHSTANFILE, ">fhstandtemp.txt") || die "Can't open $stanfile $!";
print FHSTANFILE @mdasorted;
open (FHSTANFILE, "<fhstandtemp.txt") || die "Can't open $stanfile $!";
while (<FHSTANFILE>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$pfg = sprintf("%.1f", $pfg);    ## converts to 1 place



print HIGHHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $pfg<td></tr>";
}

print HIGHHTML "</td></tr></table>";
    ### END HIGH SCORING PAGE ###
#------------------------------------------------------------------
#------------------------------------------------------------------
    ### PREPARE LEAST SCORED ON PAGE ###
print LEASTHTML "";
open (LEASTHTML, ">fhleast01.html") || die "Can't open $stanfile $!"; 
print LEASTHTML "";
print LEASTHTML "
<HTML><HEAD><title>FH Least Scored On 2001</title>
<STYLE>
td {font: 10pt arial; text-align:right}
th {font: 10pt arial; font-weight=bold; text-align:right}
</STYLE>
</head>
<BODY bgcolor=white LINK=0000ff VLINK=0000ff> <Font Face=Arial Size=-1>
<TABLE border=0  cellspacing=3 cellpadding=0 bgcolor=white>
<TR>
<TD align=center colspan=8>
<B>2001 Team Comparisons</B> sorted by:
</td></tr>   

<tr><th style=text-align:left>  Least Against   </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Apg <th></tr>";


open (FHMDA, "<fhstand.txt") || die "Can't open $stanfile $!";
@mda = <FHMDA>; 
@mdasorted = map { $_->[0] }
 sort {
      $a->[13] <=> $b->[13]   
||    $b->[10] <=> $a->[10]
||    $b->[11] <=> $a->[11]
||    $b->[14] <=> $a->[14]
||     $a->[1] cmp $b->[1] 
}
map { [ $_, (split /:/, $_,) ] } 
@mda; 


open (FHSTANFILE, ">fhstandtemp.txt") || die "Can't open $stanfile $!";
print FHSTANFILE @mdasorted;
open (FHSTANFILE, "<fhstandtemp.txt") || die "Can't open $stanfile $!";
while (<FHSTANFILE>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$pag = sprintf("%.1f", $pag);    ## converts to 1 place


print LEASTHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $pag<td></tr>";
}

print LEASTHTML "</td></tr></table>";
    ### END LEAST SCORED ON PAGE ###
#------------------------------------------------------------------
#------------------------------------------------------------------
    ### PREPARE MARGIN PAGE ###
print LEASTHTML "";
open (MARGHTML, ">fhmargin01.html") || die "Can't open $stanfile $!"; 
print MARGHTML "";
print MARGHTML "
<HTML><HEAD><title>FH Least Scored On 2001</title>
<STYLE>
td {font: 10pt arial; text-align:right}
th {font: 10pt arial; font-weight=bold; text-align:right}
</STYLE>
</head>
<BODY bgcolor=white LINK=0000ff VLINK=0000ff> <Font Face=Arial Size=-1>
<TABLE border=0  cellspacing=3 cellpadding=0 bgcolor=white>
<TR>
<TD align=center colspan=8>
<B>2001 Team Comparisons sorted by:</B>
</td></tr>

<tr><th style=text-align:left>  WL Margin   </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Mpg <th></tr>";


open (FHMDA, "<fhstand.txt") || die "Can't open $stanfile $!";
@mda = <FHMDA>; 
@mdasorted = map { $_->[0] }
 sort {
      $b->[14] <=> $a->[14]   
||    $b->[10] <=> $a->[10]
||    $b->[11] <=> $a->[11]
||     $a->[1] cmp $b->[1] 
}
map { [ $_, (split /:/, $_,) ] } 
@mda; 


open (FHSTANFILE, ">fhstandtemp.txt") || die "Can't open $stanfile $!";
print FHSTANFILE @mdasorted;
open (FHSTANFILE, "<fhstandtemp.txt") || die "Can't open $stanfile $!";
while (<FHSTANFILE>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$pmg = sprintf("%.1f", $pmg);    ## converts to 1 place


print MARGHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $pmg<td></tr>";
}

print MARGHTML "</td></tr></table>";
    ### END MARGIN PAGE ###

#------------------------------------------------------------------
#------------------------------------------------------------------
    ### PREPARE WL PERCENT PAGE ###
open (PCTHTML, ">fhpercent01.html") || die "Can't open $stanfile $!"; 
print PCTHTML "";
print PCTHTML "
<HTML><HEAD><title>FH Least Scored On 2001</title>
<STYLE>
td {font: 10pt arial; text-align:right}
th {font: 10pt arial; font-weight=bold; text-align:right}
</STYLE>
</head>
<BODY bgcolor=white LINK=0000ff VLINK=0000ff> <Font Face=Arial Size=-1>
<TABLE border=0  cellspacing=3 cellpadding=0 bgcolor=white>
<TR>
<TD align=center colspan=8>
<B>2001 Team Comparisons</B> sorted by:
</td></tr>

<tr><th style=text-align:left> WL Percent   </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";


open (FHMDA, "<fhstand.txt") || die "Can't open $stanfile $!";
@mda = <FHMDA>; 
@mdasorted = map { $_->[0] }
 sort {
      $b->[11] <=> $a->[11]   
||    $b->[10] <=> $a->[10]
||    $b->[14] <=> $a->[14]
||     $a->[1] cmp $b->[1] 
}
map { [ $_, (split /:/, $_,) ] } 
@mda; 


open (FHSTANFILE, ">fhstandtemp.txt") || die "Can't open $stanfile $!";
print FHSTANFILE @mdasorted;
open (FHSTANFILE, "<fhstandtemp.txt") || die "Can't open $stanfile $!";
while (<FHSTANFILE>) { 
	($tm,$tmconf,$tmgp,$tmw,$tml,$tmt,$tmf,$tma,$tmm,$tmpts,$tmpct,$pfg,$pag,$pmg) = split(':',$_);	

$tmpct = sprintf("%.3f", $tmpct);   ## converts to 3 places
$tmpct =~ s/^0/ /g;                 ## replace Leading Zeros

print PCTHTML "
<tr><td style=text-align:left>$tm     </td><td>$tmw</td><td>$tml</td><td>$tmt</td><td>$tmf</td><td>$tma</td><td> $tmpts</td><td> $tmpct<td></tr>";
}

print PCTHTML "</td></tr></table>";
    ### END WL PERCENT PAGE ###
#------------------------------------------------------------------
#------------------------------------------------------------------



