I'll have a go
Your response seems to work, thank you very much. Indeed, it helped me to understand the syntax involved. I used the more compact version and got the behaviours I was looking for. I'll put output and source between readmore tags.
Abridged output:
$ ./6.markov.pl markov
sub_dir is markov
path1 is /home/bob/Documents/meditations
path2 is /home/bob/Documents/meditations/markov
out_file is /home/bob/Documents/meditations/markov/my_data/22-03-2019-
+13-37-35/22-03-2019-13-37-35.1.txt
-------system out---------
I, the narrator, have endured many adventures in my life, but I really
+ thought it was all prettymuch random. That the universe were determi
+nistic never really occured to me.
I love to throw the toy with the dog! When I throw with my left hand,
+I try to follow through with extension. Sometimes I repeat with the u
+ndef hand. You never know when the toy will take a big bounce, so I k
+eep it low. It went south to the fence. Then my pretty pitty retrieve
+d it at a gallop.
.3 of the time I go to the gym. I perform swimming for my health.
----------------
out_file is /home/bob/Documents/meditations/markov/my_data/22-03-2019-
+13-37-35/22-03-2019-13-37-35.2.txt
-------system out---------
I, JAPH, have endured many Bedraengnisse in my life, but I really thou
+ght it was all prettymuch random. That the universe were deterministi
+c never really occured to me.
I love to throw the toy with the dog! When I throw with my right hand,
+ I try to follow through with extension. Sometimes I repeat with the
+undef hand. You never know when the toy will take a big bounce, so I
+keep it low. It went west. Then my pretty pitty retrieved it at a gal
+lop.
50 percent of the time I go to the gym. I perform swimming for my heal
+th.
...this plus 28 similar stories. Source:
#!/usr/bin/perl -w
use 5.011;
use Path::Tiny;
use utf8;
use open OUT => ':utf8';
use Data::Dump;
use Text::Template;
use POSIX qw(strftime);
binmode STDOUT, 'utf8';
my ($sub_dir) = $ARGV[0];
say "sub_dir is $sub_dir";
my $path1 = Path::Tiny->cwd;
say "path1 is $path1";
my $path2 = path( $path1, $sub_dir );
say "path2 is $path2";
## populate hash
my $data = [ [ 'protaganist', 'al debaran', 'the narrator', 'JAPH', '
+gilligan'],
[ 'trials' ,'adventures', 'Bedraengnisse'],
[ 'ball','toy', 'object'],
[ 'orientation' , 'left', 'right'],
[ 'dog' ,'my pretty pitty'],
[ 'num1', '.7', '.3', '50 percent'],
[ 'activity', 'stretching', 'swimming'],
[ 'non_orientation', 'undef'],
[ 'direction', 'south to the fence', 'west', 'yonder' ] ];
#dd $data;
## main loop
# set trials
my $trials = 30;
my $dummy = 1;
while ($trials > 0){
# create an output file
my $first_second = strftime( "%d-%m-%Y-%H-%M-%S", localtime );
my $out_file = $path2->child( 'my_data', "$first_second", "$first_seco
+nd\.$dummy.txt")->touchpath;
say "out_file is $out_file";
my %vars = map { $_->[0],$_->[rand($#{$_}) + 1] } @{$data};
my $rvars = \%vars;
my @pfaden = $path2->children(qr/\.txt$/);
@pfaden = sort @pfaden;
#say "paths are @pfaden ";
for my $file (@pfaden) {
#say "default is $file";
my $template = Text::Template->new(
ENCODING => 'utf8',
SOURCE => $file,
) or die "Couldn't construct template: $!";
my $result = $template->fill_in( HASH => $rvars );
$out_file->append_utf8($result);
}
say "-------system out---------";
system("cat $out_file");
say "----------------";
$trials -= 1;
$dummy += 1;
} # end while condition
__END__
So I'm happy with this as an intermediate result but started puzzling on what I was coding towards. I wanted to throw the ball to the dog and have that repeat through probabilistic methods. The repeated events were hard to extricate causally. If I tried to represent the scenario with object oriented methods, what would the "objects" be? The story, the things in the story, the state of the things in the story, the Animals as with _Intermediate Perl_? Wherein does it show Markovian nature?
After wrestling with that for a few days whilst distracted by the US div I college men's basketball tournament, I decided, wait a sec, this tournament with its "bracketology" is the type of creature I'm looking for. So I'm shifting the story from being about the dog chasing the ball to ten guys on a court chasing a ball like their lives depend on it. I'm redefining the problem here a bit and would like to change the subject to reflect that. It doesn't take long for code to get longer, so I will use readmore tags:
I intend to form the probabilities of whether a team will win from their ranking and so have munged it into their name. So far, I only have 1/4 of the teams, but I still lack the ability to simulate game play. A printable version of what I'm imitating is here First output:
$ ./3.mm.pl hoops
sub_dir is hoops
path1 is /home/bob/Documents/meditations
path2 is /home/bob/Documents/meditations/hoops
out_file is /home/bob/Documents/meditations/hoops/my_data/22-03-2019-1
+6-09-08/22-03-2019-16-09-08.1.txt
pairs are 1.duke vs 16.ndST 8.vcu vs 9.ucf 5.msST vs 12.lib 4.vaTech v
+s 13.stlouis 6.maryland vs 11.belmont 3.lsu vs 14.yale 7.louisville v
+s 10.mn 2.miST vs 15.bradley
pairs are 1.duke vs 16.ndST
matched
1 duke 16 ndST
{
event => "the Big Dance",
protaganist => "al debaran",
ref_east => [
"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",
],
region => "south",
}
-------system out---------
It is the Big Dance again, and I, al debaran, wanted to make some pred
+ictions.
I pick to win in this round of the south. Their cardinality is . loo
+ks particularly likely to win, while the underdog.
The current state of the Big Dance is .
----------------
out_file is /home/bob/Documents/meditations/hoops/my_data/22-03-2019-1
+6-09-09/22-03-2019-16-09-09.2.txt
pairs are 1.duke vs 16.ndST 8.vcu vs 9.ucf 5.msST vs 12.lib 4.vaTech v
+s 13.stlouis 6.maryland vs 11.belmont 3.lsu vs 14.yale 7.louisville v
+s 10.mn 2.miST vs 15.bradley
pairs are 1.duke vs 16.ndST
matched
1 duke 16 ndST
{
event => "hoops, baby",
protaganist => "JAPH",
ref_east => [
"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",
],
region => "south",
}
-------system out---------
It is hoops, baby again, and I, JAPH, wanted to make some predictions.
+
I pick to win in this round of the south. Their cardinality is . loo
+ks particularly likely to win, while the underdog.
The current state of hoops, baby is .
----------------
$
It looks like I pair them off properly, but I only get one team to match the ensuing regex. I kind of get lost there, which is not a good sign when you're writing your own code. Here is the current script; the entire workspace with scripts and data files is still on github. (Now 6.1 kb)
#!/usr/bin/perl -w
use 5.011;
use Path::Tiny;
use utf8;
use open OUT => ':utf8';
use Data::Dump;
use Text::Template;
use POSIX qw(strftime);
binmode STDOUT, 'utf8';
my ($sub_dir) = $ARGV[0];
say "sub_dir is $sub_dir";
my $path1 = Path::Tiny->cwd;
say "path1 is $path1";
my $path2 = path( $path1, $sub_dir );
say "path2 is $path2";
## populate hash
my $data = [
[ 'protaganist', 'al debaran', 'the narrator', 'JAPH', 'Dick Vitale'
+ ],
[ 'event', 'March Madness', 'the Big Dance', 'hoops, baby' ],
[ 'region', 'east', 'west', 'south', 'midwest' ]
];
#dd $data;
## main loop
# set trials
my $trials = 2;
my $dummy = 1;
while ( $trials > 0 ) {
# create an output file
my $first_second = strftime( "%d-%m-%Y-%H-%M-%S", localtime );
my $out_file =
$path2->child( 'my_data', "$first_second", "$first_second\.$dummy.
+txt" )
->touchpath;
say "out_file is $out_file";
# stochastic input of appositives
my %vars = map { $_->[0], $_->[ rand( $#{$_} ) + 1 ] } @{$data};
my $rvars = \%vars;
$rvars = pop_brackets($rvars);
$rvars = calc_winners($rvars);
dd $rvars;
my @pfade = $path2->children(qr/\.txt$/);
@pfade = sort @pfade;
#say "paths are @pfade";
for my $file (@pfade) {
#say "default is $file";
my $template = Text::Template->new(
ENCODING => 'utf8',
SOURCE => $file,
) or die "Couldn't construct template: $!";
my $result = $template->fill_in( HASH => $rvars );
$out_file->append_utf8($result);
}
say "-------system out---------";
system("cat $out_file");
say "----------------";
$trials -= 1;
$dummy += 1;
} # end while condition
sub pop_brackets {
my $rvars = shift;
my %vars = %$rvars;
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);
$vars{ref_east} = \@east;
return \%vars;
}
sub calc_winners {
use 5.016;
use warnings;
my $rvars = shift;
my %vars = %$rvars;
my $new_ref = $vars{ref_east};
my @east = @$new_ref;
#say "east is @east";
my @pairs;
while (@east) {
my $first = shift @east;
my $next = shift @east;
push @pairs, "$first vs $next";
}
say "pairs are @pairs";
my @winners = play_game(@pairs);
return \%vars; # end calc_winners
}
sub play_game {
use 5.016;
use warnings;
my @pairs = shift;
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;
my $random_number = rand();
if ( $random_number < $ratio ) {
push @winners, "$1.$2";
}
else {
push @winners, "$3.$4";
}
}
}
return @winners;
} # end play_game
__END__
Also, I'm fishing for any way to re-imagine this problem. I'm gonna have to create a dozen hodge-podge arrays, and the tournament is always a 2**6 thing.*
Thanks for your comment,
*except for "first four" games
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|