Hello Monks,
I've written a script that attempts to run simulations of the US college men's basketball tournament. What I have "works," representing this tournament as I understand it until I try to use File::Find to see what I've got. I'll put abridged output and source between readmore tags for interested parties.
Output of command ./7.64.pl
round is 1 1.duke, 9.ucf, 5.msST, 13.stlouis, 6.maryland, 3.lsu, 7.louisville, 2. +miST 1.gonzaga, 8.syracuse, 12.murrayST, 4.flaST, 11.azST, 3.texTech, 7.nev +ada, 2.mi 1.va, 9.ok, 5.wi, 4.ksST, 11.stmarys, 3.purdue, 7.cincy, 2.tn 1.nc, 9.wa, 5.auburn, 4.ks, 11.ohST, 3.houston, 7.wofford, 2.ky round is 2 1.duke, 5.msST, 3.lsu, 2.miST 1.gonzaga, 4.flaST, 3.texTech, 2.mi 1.va, 4.ksST, 3.purdue, 2.tn 1.nc, 4.ks, 3.houston, 2.ky round is 3 1.duke, 3.lsu 1.gonzaga, 2.mi 1.va, 2.tn 4.ks, 2.ky round is 4 3.lsu 1.gonzaga 1.va 4.ks finals are 1.gonzaga 1.va tournament winner is 1.va ---------------- $
Source listing of 7.64.pl :
#!/usr/bin/perl -w use 5.011; use Path::Tiny; use utf8; use open OUT => ':utf8'; use Data::Dump; use POSIX qw(strftime); binmode STDOUT, 'utf8'; # whereamI my $path1 = Path::Tiny->cwd; say "path1 is $path1"; my @region = ( 'east', 'west', 'south', 'midwest' ); #4 different b +rackets ## main control # set trials my $parent; my $trials = 15; my $dummy = 1; my $first_second = strftime( "%d-%m-%Y-%H-%M-%S", localtime ); while ( $trials > 0 ) { # unique point at which probability is assigned for teams. my $ref_bracket = pop_brackets(); my %vars = %$ref_bracket; my $rvars = \%vars; # create an output file my $out_file = $path1->child( 'my_data', "$first_second", "$first_second\.$dummy. +txt" ) ->touchpath; $parent = $out_file->parent; say "out_file is $out_file"; my $teams_left = 16; my $round = 1; my @final_four; while ( $teams_left > 1 ) { say "round is $round"; $out_file->append_utf8( "round is $round", "\n" ); my $anzahl = 2; for my $r (@region) { say "r is $r"; my $ref_calc = calc_winners( $rvars, $r ); dd $ref_calc; $vars{$r} = $ref_calc; #update regional bracket with winners my @sieger = @$ref_calc; say "winners are @sieger"; $anzahl = scalar @sieger; if ( $anzahl == 1 ) { push @final_four, $sieger[0]; } my $string_sieger = join( ', ', @sieger ); say "string sieger is $string_sieger"; $out_file->append_utf8( $string_sieger, "\n" ); } $dummy += 1; $round++; $teams_left = $anzahl; say "final four are @final_four"; } #end for loop my $ref_finals = final_four( \@final_four ); my @finals = @$ref_finals; say "finals are @finals"; $out_file->append_utf8( "finals are @finals", "\n" ); my $ref_gewinner = final_four( \@finals ); my @gewinner = @$ref_gewinner; my $last = $gewinner[0]; say "tournament winner is $last"; $out_file->append_utf8( "tournament winner is $last", "\n" ); say "-------system out---------"; system("cat $out_file"); say "----------------"; $trials--; } #end while loop ## see what we got use File::Find; # Get $dirname from first command-line argument my $dirname = $parent; find( \&do_process, $dirname ); my ( $a, $b ); sub do_process { if ( -r $_ ) { my $file_name = $_; open( my $fh, '<', $file_name ); # Use three-arg open! while (<$fh>) { chomp(); if (/\btournament winner is 1.gonzaga\b/i) { $a = "$file_name:$_ +"; } if (/\btournament winner is 2.miSTb/i) { $b = "$file_name:$_ +"; } } } } sub pop_brackets { use 5.016; use warnings; my %vars; my @east = qw(1.duke 16.ndST 8.vcu 9.ucf 5.msST 12.lib 4.vaTech 13.stlouis 6. +maryland 11.belmont 3.lsu 14.yale 7.louisville 10.mn 2.miST 15.bradley); my @west = qw(1.gonzaga 16.farleigh 8.syracuse 9.baylor 5.marquette 12.murray +ST 4.flaST 13.vermont 6.buffalo 11.azST 3.texTech 14.noKY 7.nevada 10.fla 2.mi 15.montana); my @south = qw(1.va 16.gardner 8.ms 9.ok 5.wi 12.or 4.ksST 13.UCirv +6.nova 11.stmarys 3.purdue 14.olddominion 7.cincy 10.iowa 2.tn 15.colgate +); my @midwest = qw(1.nc 16.iona 8.utST 9.wa 5.auburn 12.nmST 4.ks 13.n +e 6.iowaST 11.ohST 3.houston 14.gaST 7.wofford 10.setonhall 2.ky 15.abilene); $vars{east} = \@east; $vars{west} = \@west; $vars{south} = \@south; $vars{midwest} = \@midwest; return \%vars; } sub calc_winners { use 5.016; use warnings; use Data::Dump; my ( $rvars, $region ) = (@_); my %vars = %$rvars; my $new_ref = $vars{$region}; my @teams = @$new_ref; my @pairs; while (@teams) { my $first = shift @teams; my $next = shift @teams; push @pairs, "$first vs $next"; } #say "pairs are @pairs"; my $ref_pairs = \@pairs; my $ref_winners = play_game($ref_pairs); return $ref_winners; # end calc_winners } sub play_game { use 5.016; use warnings; my $ref_pairs = shift; my @pairs = @$ref_pairs; say "in play_game"; #say "pairs are @pairs"; my @winners; for my $line (@pairs) { if ( $line =~ /^(\d+)\.(\w+) vs (\d+)\.(\w+)$/ ) { #say "matched"; #say "$1 $2 $3 $4"; my $denominator = $1 + $3; my $ratio = $3 / $denominator; #say "ratio was $ratio"; my $random_number = rand(); if ( $random_number < $ratio ) { push @winners, "$1.$2"; } else { push @winners, "$3.$4"; } } } my $ref_winners = \@winners; return $ref_winners; } # end play_game sub final_four { use 5.016; use warnings; use Data::Dump; my ($new_ref) = (@_); my @teams = @$new_ref; my @pairs; while (@teams) { my $first = shift @teams; my $next = shift @teams; push @pairs, "$first vs $next"; } say "pairs are @pairs"; my $ref_pairs = \@pairs; my $ref_winners = play_game($ref_pairs); return $ref_winners; # final_four } __END__
How do I imitate the grep functionality with perl's File::Find? I expected to see files matched, as 1.gonzaga did win one of those simulated tournaments. These do serve as an SSCCE if you're open to having a governable amount of data created in a mydata/ subdirectory. You can turn trials down to 1 or 2 if you don't want much output.
As much as I don't want Nike, gambling, and unrealized social justice to get in the way of a Spring Classic, I realize that it means as much to you as you decide. That there is a tennis shoe intrigue this time is not normal, the way so much of american life is not normal now.
As for me, I would rather simulate the tourney using perl than gamble on it. But I would be less than sporting if I didn't try to predict a winner, and I'll go with 2.miST .
Thanks for your comment
In reply to using File::Find to grep for text by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |