Ok I sent you a pull request, here's some changes I would make in the syntax
-
default options in case user does not specify
-
hash slice
- ternary operator
- eliminate superfluous variables
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,$anzah
+l,$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"
}
}
None of those really change anything, just make it "cleaner". What's better, a doctor that cures with more medicine or less?
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.
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.)
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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.