#!/usr/bin/perl -s

# determine Order
$keyLen = 3;
$keyLen = $k if ($k ne '');
$initKey = pack "a$keyLen", "                             ";

# determine Starting Phrase
$startKey = "Dear God, ";
$startKey = $s if ($s ne '');

print "InitKey: <$initKey>  k=$k\n";

# Collect String
while (<>)
{
	$instr .= $_;
}

# Construct Markov Chain
$key = $initKey;
while ($instr ne '')
{
	$ch = substr($instr, 0, 1);
	$ch = ' ' if $ch =~ /\s/;
	$instr = substr($instr,1);
	$mark{$key} .= $ch;
	$key = substr($key,1) . $ch;
}
foreach $x (0..length($key)-1)
{
	$mark{$key} .= ' ';
	$key = substr($key,1) . ' ';
}


# Output Sample
$key = pack "a$keyLen", $startKey;
print $key;

$n = 0;
foreach $x (0..2000)
{
	$ch = substr($mark{$key},int(rand(length($mark{$key}))),1);
	if ($ch eq ' ' && $n > 70)
  	{
		$n = 0;
		print "\n";
  	}
	else {
		print $ch;
	}
	++$n;
	$key = substr($key,1) . $ch;
}

#end