Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm trying something new that I've been bouncing around in my head since bliako posted his markovian meditation n-dimensional statistical analysis of DNA sequences (or text, or ...). bliako has all these interesting and abstruse scripts that are right on the edge of what I can replicate. I was able to do so once with the Markovian Frankentext of that thread but could not do it again. I want to reach for something much simpler, indeed as simple as I can imagine it as an SSCCE. With that as an introduction, I'll put a statement of the problem in readmore tags:

In 1972, we could order things called "Mad Libs" which were text templates for writing stories. I would like to create one with stochastic inputs. The templates are read as files using Path::Tiny after which they undergo the substitutions using Text::Template. The lexicographic order provides a basic cause/effect continuum that makes sense. I have parts of this written, hopefully well enough so that I describe what I seek.

I worked up an example on github. It is 2.8k now and I will say that it is less than 50 k and hope never to have to update this line. These programs can generate a lot of story pretty quickly, so I'll do my part to keep bandwidth to modem levels. I present output:

$ pwd /home/bob/Downloads/Markov-master $ cx #alias for chmod +x $ ./ markov/ sub_dir is markov/ path1 is /home/bob/Downloads/Markov-master path2 is /home/bob/Downloads/Markov-master/markov out_file is /home/bob/Downloads/Markov-master/markov/my_data/20-03-201 +9-14-27-36.txt.txt -------system out--------- I, al debaran, have endured many adventures in my life, but I really t +hought it was all prettymuch random. That the universe were determini +stic 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. .7 of the time I go to the gym. I perform stretching for my health. ---------------- [ ["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"], ] $

then 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"; # create an output file my $munge = strftime( "%d-%m-%Y-%H-%M-%S\.txt", localtime ); my $out_file = $path2->child( 'my_data', "$munge" )->touchpath; say "out_file is $out_file"; ## populate hash my %vars = ( protaganist => 'al debaran', trials => 'adventures', ball => 'toy', orientation => 'left', dog => 'my pretty pitty', num1 => '.7', activity => 'stretching', non_orientation => 'undef', direction => 'south to the fence', ); 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 "----------------"; 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; __END__

So, I'm getting good intermediary results. What I want to do is map the values from $data to populate %var instead. The first column is the keys of the hash. Those that follow are probable values. For starters, I would like to make them equaprobable, so a mapping function would get a pseudorandom on the unit interval, and multiply it by the cardinality of the possibilities. I haven't done that in perl, so I'm fishing for ways to do that.

Also, I'm looking for a little logic that returns the compliment of left and right in 'orientation'. I've got ways to do these things, they just all look like leftover fortran and C. I'm looking for perl solutions. Maybe I need to represent these data entirely differently....

Thanks for your comment