note
trippledubs
<p>Ok I sent you a pull request, here's some changes I would make in the syntax</p>
<ul>
<li>
default options in case user does not specify
</li>
<li>
hash slice
</li>
<li>ternary operator</li>
<li>eliminate superfluous variables </li>
</ul>
<code>
diff --git a/7.mm.pl b/7.mm.pl
old mode 100644
new mode 100755
index 84efc24..7db8750
--- a/7.mm.pl
+++ b/7.mm.pl
@@ -8,7 +8,7 @@ use Text::Template;
use POSIX qw(strftime);
binmode STDOUT, 'utf8';
-my ($sub_dir) = $ARGV[0];
+my ($sub_dir) = $ARGV[0] || 'out';
say "sub_dir is $sub_dir";
my $path1 = Path::Tiny->cwd;
say "path1 is $path1";
@@ -61,14 +61,11 @@ while ( $trials > 0 ) {
my %vars = map { $_->[0], $_->[ rand( $#{$_} ) + 1 ] } @{$data};
# further stochastic output from "playing" the games
- $vars{"winners"}=$string_sieger;
- $vars{"cardinality"}=$anzahl;
- $vars{"region"}=$r;
+ @vars{qw/winners cardinality region/} = ($string_sieger,$anzahl,$r);
my $rvars = \%vars; #important
- my @pfade = $path2->children(qr/\.txt$/);
- @pfade = sort @pfade;
+ my @pfade = sort $path2->children(qr/\.txt$/);
#say "paths are @pfade";
@@ -80,8 +77,7 @@ while ( $trials > 0 ) {
SOURCE => $file,
) or die "Couldn't construct template: $!";
- my $result = $template->fill_in( HASH => $rvars );
- $out_file->append_utf8($result);
+ $out_file->append_utf8($template->fill_in( HASH => $rvars));
}
say "-------system out---------";
system("cat $out_file");
@@ -167,14 +163,7 @@ sub play_game {
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";
- }
-
+ push @winners, rand() < $ratio ? "$1.$2" : "$3.$4"
}
}
</code>
<p>None of those really change anything, just make it "cleaner". What's better, a doctor that cures with more medicine or less? </p>
<p>
The data structure you use is a string '$rank.$team', that's not good! The rank should be a property of the team and division. And really if you think object oriented you have teams, divisions, games, lots of directions to go. You need a really flexible win_predictor() function or class, because it's likely to grow a lot. And then the templating is almost a totally separate thing, which is also going to change.
</p>
<p>
<i>What this shows is that I have too much repitition of the introduction and the summary. I also don't have any mechanisms for going beyond round one. (Gladly taking suggestions on how I might do that.)</i>
</p>
<p>
in play_game(), you have to decouple the data to do the prediction, bad. But it's almost recursive already. If you don't change the data structure, you need to put it back in the form the sub expects and just call play_game(\@winners). I guess bye rounds will make that a little trickier.
</p>
1231512
1231840