#!/usr/bin/perl
use warnings;
use strict;
my ($record, @aoh, $key, %hash);
%hash = ();
while(<>){
my @fields = split /^(\w+\s*\w+)\s*/mi; #the whole crux of the "magic" ;)
shift @fields;
push(@aoh, {map /(.*)/, @fields}); #Thank you Perl CookBook!! :D
}
foreach $record (@aoh) {
for $key (sort keys %$record) {
if(exists $record->{$key}) {
push(@{$hash{$key}}, $record->{$key});
}
else {
push(@{$hash{$key}}, $record->{$key});
}
}
}
my @keys = keys %hash;
my $seed = "And he"; #seed value :/
my $flag;
#print "$seed $hash{$seed}->[$flag]\n";
for (keys %hash) {
if(exists $hash{$seed}){
my @val = @{$hash{$seed}};
print "$seed $val[rand @val]\n";
#now we set the new seed
$seed = "$seed $val[rand @val]";
$seed =~ /^\w+\s*(\w+\s*\w+)\s*/mig;
$seed = $1;
}
else {
$seed = $keys[rand @keys];
}
}