Friday, September 10, 2010

junk code

Today I was tidying up my junk code directory. I cut this up one lunch time when I couldn't focus on the FTP server code I was bashing my head against - now sorted.

So, whats this for? There was a challenge a while back on the Brainwagon blog, using /dev/urandom to generate a list of 'random' characters to fit a pattern, ten blocks of five characters, each block separated by spaces.

This is used for testing cyphers and other fun stuff =) I spent some time when I was back in London working on some cypher and compression code. So I can vouch for the need for tools to generate large random sets - one time pads - that you can play back over and over to make sure that you reproduce that bug, with different versions of your code. Correctness was the issue at hand. Later came the performance issues.

So, Mark posted a "Unix/sh challenge". My brain needed a break and who can resist a bit lunch time shell coding? Well I tried and I got it pretty close. Anyway I decided to try a Perl version to see if I could make it easier to read, then make it faster.


#!/usr/bin/perl
my $limit = 10_000;
my @cypher;
while (<>) {
my @chars = split(undef, $_);
for my $char (@chars) {
if ($char =~ m/[A-Z]/ ) {
push @cypher, $char;
if ($limit <= scalar(@cypher)) {
my $offest = 0;
while (@cypher) {
for (1..10) {
print " ";
for (1..5) {
print pop @cypher;
}
}
print "\n";
}
exit;
}
}
}
}


72, Kim

No comments:

Post a Comment