Microblog: A very long article Wikipedia article on the orientation of toilet paper [Jun 7th, 22:52] [R]

Monday, October 1st, 2007

More Dirty Things to do in Perl: Unsorting

Categories: [ IT ]

I discussed today with Matthieu Fritz about how the act of reading was processed in the brain, and he reminded me of a chain e-mail containing text where the letters of the words were scrambled, except the first one and the last one, and which was still surprisingly readable. He wondered if it worked with any text, or if the text in the e-mail was carefully composed. I quickly hacked a Perl script doing the job, and in the middle of writing it, I was wondering how I could scramble the letters of a word without writing too much code. The the solution popped into my mind: use sort, unsing rand instead of a comparison function. Here's the resulting script:

sub scramble {
  my $s = shift @_;
  my @letters = split //,$s;
  my $first = shift @letters;
  # Do not scramble words starting with an uppercase letter
  return $s if $first =˜ /[A-Z]/;
  my $last = pop @letters;
  my $rand = int(rand(2));
  # $rand == 0 means that the letters won't be exchanged, but we want as much
  # scrambling as possible, therefore we discard the use of 0
  $rand = -1 if $rand == 0;
  my @l = sort { $rand } @letters;
  return "$first".join("", @l)."$last";
}
 
while (<>) {
  # Scramble the words composed of lowercase letters and accentuated
  # characters
  s/([a-z\xa0-\xff]+)/&scramble($1)/ge;
  print;
}

Verdict: it works with short and common words, but not with longer or rarers ones.

[ Posted on October 1st, 2007 at 20:39 | no comment | ]

Trackback Address

https://weber.fi.eu.org/blog/Informatique/more_dirty_things_to_do_in_perl.trackback

Comments

No comment

Add comments

You can use the following HTML tags: <p>, <br>, <em> <strong>, <pre>. URLs starting with http:// will automatically be turned into hyperlinks.

(optional)
(optional)


Save my Name and URL/Email for next time

7 - 2 =