Article 2389 of comp.lang.perl:
Xref: feenix.metronet.com rec.sport.baseball:5975 comp.lang.perl:2389
Newsgroups: su.sports,rec.sport.baseball,comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!wupost!howland.reston.ans.net!agate!headwall.Stanford.EDU!nntp.Stanford.EDU!nntp!stergios
From: stergios@leland.Stanford.EDU (Stergios)
Subject: Baseball Division Picker Program
Message-ID: <STERGIOS.93Apr24114130@kt22.Stanford.EDU>
Sender: news@leland.Stanford.EDU (Mr News)
Reply-To: stergios@kt22.Stanford.EDU
Organization: DSO, Stanford University
Date: 24 Apr 93 11:41:30
Lines: 144



Here's a perl program to pick the division standings for Major League
Baseball.

// sm
// Stergios Marinopoulos   Sat Apr 24 11:38:57 1993
// stergios@kt22.Stanford.EDU


#!/usr/local/bin/perl

### a perl program to pick the division standings for Major League Baseball

### this program rips right through the al east and al west, but really 
### slows down in the national league due to the expansion team odds 
### being so high.  You could always reduce the odds to something like 
### 1000 to 1, instead of 10,000 to 1.  The real odds were 100M to 1!

### The data used in the following program was Danny Sheirdan's odds
### against winning the AL & NL pennants and World Series as they appeared
### in USA today on Monday April 5 1993.  The odds were listed by league
### and not by division.  I simply broke the list into divisons without
### changing the odds, and almost certainly introducing errors.  The
### expansion team odds where actually 100M to 1.  I reduced them to 10000
### to 1 in the interest of speed.  Reducing them to 1000 to 1 would
### probably be good enough.


### sm
### Stergios Marinopoulos   Sat Apr 24 11:10:50 1993
### stergios@kt22.Stanford.EDU

$nl = "\n" ;

### define the associative arrays for each division, where the key
### is the team name, and the value is the that team's odds to win the
### pennant in their league.

%alw = ('Chicago',	'4',
	'Minnesota',	'5',
	'Oakland',	'10',
	'Kansas City',	'12',
	'Texas',	'15',
	'Seattle',	'50',
	'California',	'100'
	) ;

%ale = ('Toronto',	'3',
	'Baltimore',	'5',
	'New York',	'7',
	'Milwaukee',	'15',
	'Cleveland',	'20',
	'Boston',	'25',
	'Detroit',	'75'
	) ;


%nlw = ('Atlanta',	'2',
	'Cincinnati',	'4',
	'Houston',	'15',
	'San Francisco','20',
	'San Diego',	'25',
	'Los Angeles',	'25',
	'Colorado',	'10000'
	) ;

%nle = ('Montreal',	'6',
	'New York',	'7',
	'Philadelphia',	'8',
	'St. Louis',	'10',
	'Chicago',	'30',
	'Pittsburgh',	'30',
	'Florida',	'10000'
	) ;


srand(time|$$) ;

## define a list with the names of the divisions
@divNames = ("AL EAST", "AL WEST", "NL EAST", "NL WEST") ;

## define a list with pointers to the division associative arrays
@divPointers = (*ale, *alw, *nle, *nlw) ;


### pick the divison standings

foreach $div (@divNames) {
    local(*division) = shift(@divPointers) ;

    print $nl, $div, $nl ;

    foreach $t (0..6) {
		## it was easier to fill up the hat each time, rather
		## than sift out a team that was just picked.
		&fillUpHat ;
		$pick = &pickATeam ;
		printf "%-3d $pick\n", 7 - $t ;

		## since this team has been picked,remove it division
		undef $division{$pick} ;
    }
}

### that's it. we're out of here.
exit 0 ;

sub fillUpHat {
    # fill up a list ("HAT") with the names of the teams
    # remaining in the division.  Each team's name is entered
    # their "odds" number of times.

    @list = () ;
    foreach $team (keys %division) {
	grep(push(@list, $team), 0..$division{$team}) ;
    }
}

sub pickATeam {
    @wlist = @list ;		# The work list
    @rlist = () ;		# The random list

    ## first randomize the list, i.e. get the air blowing 
    ## in the ping pong ball container.

    while (1) {
	$i = int(rand($#wlist)) ;
	push(@rlist, $wlist[$i]) ;
	splice(@wlist, $i, 1) ;
	if($#wlist == 0) {
	    push(@rlist, @wlist) ;
	    last ;
	}
    }
    
    ## now pick the team
    $rlist[int(rand($#rlist))] ;
}

--
--------------------------
Stergios Marinopoulos
stergios@kt22.Stanford.EDU


