# FILE fbscocomb.pl  working copy  running time 17 sec

#============= CREATE SCORES PAGE ===================
$scofile='FBsco01.txt'; 
open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
open (SCORESHTML, ">fbscores01.html");
print SCORESHTML "<html><head><title>CIAU football scores 2001</title>
</head>
<body>
<Font Face=Arial size=-1>\n
<big><b>FB Scores 2001</b></big>";

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";
}
                                        # dispose of heading in data file
if ($date =~ /CIAU/) {
print SCORESHTML "<big><b>$date</b></big>\n";   next;
}
                          # print date if alone on datafile row
if  ($Tm1 le " ") {   
  print SCORESHTML "<br><b>$date</b>\n";
                 }              
                          #print score lines without dates
else {                    
print SCORESHTML "<br>$Tm1 $Sco1 $Tm2 $Sco2 $GmType $Notes\n";
     }
}
print SCORESHTML "</body></html>"; #close HTML file
close SCORESHTML;

#====================================================
#### TEAM-BY-TEAM SCORES ###
#====================================================

$scofile='FBsco01.txt'; 
open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
open (TEAMDATA, ">fbteams01.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 FB SCORES by Team</B><small>

<TR>
<TD  NOWRAP align=center valign=top BGCOLOR=\"\#d2d5e1\">
<B>Atlantic</B><Br><B>AUS</B><Br>
<A HREF=\"\#Acadia\">Acadia</A><Br>
<A HREF=\"\#Mt Allison\">Mt Allison</A><Br>
<A HREF=\"\#Saint Mary's\">Saint Mary's</A><Br>
<A HREF=\"\#StFX\">StFX</A><Br>
<TD  align=center valign=top BGCOLOR=\"\#d2d5e1\">
<B>Quebec</B><Br><B>QIFC</B><Br>
<A HREF=\"\#Bishop's\">Bishop's</A><Br>
<A HREF=\"\#Concordia\">Concordia</A><Br>
<A HREF=\"\#Laval\">Laval</A><Br>
<A HREF=\"\#McGill\">McGill</A><Br>

<TD align=center valign=top BGCOLOR=\"\#d2d5e1\"><B> Ontario </B><Br>
<B>OUA</B><Br>
<A HREF=\"\#Guelph\">Guelph</A><Br>
<A HREF=\"\#Laurier\">Laurier</A><Br>
<A HREF=\"\#McMaster\">McMaster</A><Br>
<A HREF=\"\#Ottawa\">Ottawa</A><Br>
<A HREF=\"\#Queen's\">Queen's</A><Br>
<A HREF=\"\#Toronto\">Toronto</A><Br>
<A HREF=\"\#Waterloo\">Waterloo</A><Br>
<A HREF=\"\#Western\">Western</A><Br>
<A HREF=\"\#Windsor\">Windsor</A><Br>
<A HREF=\"\#York\">York</A><Br>

<TD  align=center valign=top BGCOLOR=\"\#d2d5e1\">
<B>Canada West</B><Br><B>CWUAA</B><Br>
<A HREF=\"\#Alberta\">Alberta</A><Br>
<A HREF=\"\#Calgary\">Calgary</A><Br>
<A HREF=\"\#Manitoba\">Manitoba</A><Br>
<A HREF=\"\#Regina\">Regina</A><Br>
<A HREF=\"\#Saskatchewan\">Saskatchewan</A><Br>
<A HREF=\"\#UBC\">UBC</A><Br>
</TR></TD></TABLE>
&nbsp;
";

@TeamName = ("Acadia", "Mt Allison", "Saint Mary's", "StFX",
"Bishop's", "Concordia", "Laval", "McGill",
"Guelph", "Laurier", "McMaster", "Ottawa", "Queen's", 
"Toronto", "Waterloo", "Western", "Windsor", "York",   
"Alberta", "Calgary", "Regina", "Saskatchewan", "Manitoba", 
"UBC",);

foreach $team (@TeamName) {
open (TEAMDATA, ">>fbteams01.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='FBsco01.txt'; 
 open (FBMDA, ">fbstand.txt") || die "Can't open fbstand.txt $!"; 
print FBMDA "";

@TeamName = ("Acadia", "Mt Allison", "Saint Mary's", "StFX",
"Bishop's", "Concordia", "Laval", "McGill",
"Guelph", "Laurier", "McMaster", "Ottawa", "Queen's", 
"Toronto", "Waterloo", "Western", "Windsor", "York",   
"Alberta", "Calgary", "Regina", "Saskatchewan", "Manitoba", 
"UBC",);

foreach $team (@TeamName) {
  open (SCORES,"<$scofile") || die "Can't open $scorefile $!";
  open (FBMDA, ">>fbstand.txt") || die "Can't open fbstand.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;  
$olosses=0;  
              # while reading SCORES (FBsco01.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 
}


         #=================================================
if ($Tm2 eq $team && $Notes =~ /OL/) {      # OL Overtime Loss
++$olosses ;


}
         #=================================================

$Margin = ($pfor - $Against);        # Calculates +/- diff
$Pts = (2 * $teamwins) + $teamties + $olosses; 


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 =~ /Acadia|Mt Allison|Saint Mary's|StFX/) {   
   $conf = "aAU";
   }
elsif ($team =~ /Bishop's|Concordia|Laval|McGill/) {
   $conf = "bQU";
   }
elsif ($team =~ /Guelph|Laurier|McMaster|Ottawa|Queen's|
Toronto|Waterloo|Western|Windsor|York/) {
   $conf = "cOU";
   }
elsif ($team =~ /Alberta|Calgary|Manitoba|Regina|Saskatchewan|UBC/) {
   $conf = "dCW";
   }
}   
            # END calculation of standings variables for Team.

print FBMDA "$team:$conf:$teamgp:$teamwins:$teamlosses:$teamties:$pfor:$Against:$Margin:$Pts:$WLpct:$pfpg:$papg:$mgp\n";
}
close FBMDA;

#====================================================
#### SORTING STANDINGS ###
#====================================================

$stanfile='fbstand.txt'; 
open (FBMDA, "<fbstand.txt") || die "Can't open $stanfile $!"; 

@mda = <FBMDA>; 
@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, ">fbstand01.html");
print STANHTM "";
open (STANHTML, ">>fbstand01.html");

open (STANAUS, ">fbstanaus.txt");
open (STANQUE, ">fbstanque.txt");
open (STANOUA, ">fbstanoua.txt");
open (STANCW, ">fbstancw.txt");

print STANAUS @mdasorted[0 .. 3];  ##AUS .. begins zero
print STANQUE @mdasorted[4 .. 7];  ##Quebec
print STANOUA @mdasorted[8 .. 17];  ##OUA
print STANCW @mdasorted[18 .. 24];  ##CanWest

##======================================================
print STANHTML "
<HTML><HEAD><title>FB 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 Football 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, "<fbstanaus.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>";
}
##================QUE=======================

print STANHTML "
<tr><th style=text-align:left> Quebec </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANQUE, "<fbstanque.txt");

while (<STANQUE>) { 
	($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>";
}

##================OUA=======================

print STANHTML "
<tr><th style=text-align:left> Ontario </th><th>W </th><th>L </th><th>T </th><th>  F  </th><th>   A </th><th>  P </th><th>Pct <th></tr>";

open (STANOUA, "<fbstanoua.txt");

while (<STANOUA>) { 
	($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, "<fbstancw.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>";
print STANHTML "OL= Overtime Loss add 1 pt in CW";
    ### 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, ">fbhigh01.html") || die "Can't open $stanfile $!"; 
print HIGHHTML "";
print HIGHHTML "
<HTML><HEAD><title>FB 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 (FBMDA, "<fbstand.txt") || die "Can't open $stanfile $!";
@mda = <FBMDA>; 
@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 (FBSTANFILE, ">fbstandtemp.txt") || die "Can't open $stanfile $!";
print FBSTANFILE @mdasorted;
open (FBSTANFILE, "<fbstandtemp.txt") || die "Can't open $stanfile $!";
while (<FBSTANFILE>) { 
	($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, ">fbleast01.html") || die "Can't open $stanfile $!"; 
print LEASTHTML "";
print LEASTHTML "
<HTML><HEAD><title>FB 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 (FBMDA, "<fbstand.txt") || die "Can't open $stanfile $!";
@mda = <FBMDA>; 
@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 (FBSTANFILE, ">fbstandtemp.txt") || die "Can't open $stanfile $!";
print FBSTANFILE @mdasorted;
open (FBSTANFILE, "<fbstandtemp.txt") || die "Can't open $stanfile $!";
while (<FBSTANFILE>) { 
	($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, ">fbmargin01.html") || die "Can't open $stanfile $!"; 
print MARGHTML "";
print MARGHTML "
<HTML><HEAD><title>FB 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 (FBMDA, "<fbstand.txt") || die "Can't open $stanfile $!";
@mda = <FBMDA>; 
@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 (FBSTANFILE, ">fbstandtemp.txt") || die "Can't open $stanfile $!";
print FBSTANFILE @mdasorted;
open (FBSTANFILE, "<fbstandtemp.txt") || die "Can't open $stanfile $!";
while (<FBSTANFILE>) { 
	($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, ">fbpercent01.html") || die "Can't open $stanfile $!"; 
print PCTHTML "";
print PCTHTML "
<HTML><HEAD><title>FB 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 (FBMDA, "<fbstand.txt") || die "Can't open $stanfile $!";
@mda = <FBMDA>; 
@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 (FBSTANFILE, ">fbstandtemp.txt") || die "Can't open $stanfile $!";
print FBSTANFILE @mdasorted;
open (FBSTANFILE, "<fbstandtemp.txt") || die "Can't open $stanfile $!";
while (<FBSTANFILE>) { 
	($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 ###
#------------------------------------------------------------------
#------------------------------------------------------------------



