Wednesday, March 12, 2008

PERL for fast and ugly scripts :)

Yup. Example below. Data generator for dishonest casino example from "Biological sequences analysis". Yes I know that states are hardly changeable :). I am putting this code here also to hear answer for this question. how to produce nice colored html output from Eclipse. Eclipse -> Open office -> blogger is quite disappointing solution.




$outputName = $ARGV[0];
$dices = $ARGV[1];

@fair = (1,2,3,4,5,6);
@loaded = (1,2,3,4,5,6,6,6,6,6);

%model = (
"f" => \@fair,
"l" => \@loaded
);

%change = (
"f" => .05,
"l" => .1
);
if(rand() > .5) {
$actual = "l";
$next = "f";
} else {
$actual = "f";
$next = "l";
}
open(F, '>', $outputName) or die "file $outputName cannot be opened\n";

select F;

for($i = 0; $i < $dices; ++$i) {
my @a = @{$model{$actual}};
print $a[(rand() * @a) % @a], " ", $actual, "\n";
if(rand() <= $change{$actual}) {
($actual, $next) = ($next, $actual);
}
}

close(F);


No comments: