Box Relatives

Thoughts about puzzles, math, coding, and miscellaneous

Deprecated perl

| 12 Comments

Perl, I’m leaving you.

I changed my GMail status to this today and I thought I would give a few words about why. I wanted to go through my Scrabble word list to find five-letter words in which the 3rd and 4th letters were the same, and which became new words when read backward. At first I modified a stock perl script of mine to do it, and then I wondered how easy it would be in Python. So I took a few minutes and came up with this:

#!/usr/bin/python

fid = open('scrabble.txt','r')
dict = [x.rstrip('\r\n') for x in fid.readlines()]
fid.close()
mywords = [x for x in dict if len(x) == 5 and \
x[2] == x[3] and x[::-1] in dict]
print mywords

(Edited to fix a minor error)

How awesome is that? Look at how short, clean, and readable it is! The only slightly tricky part is the

x[::-1]

bit that reverses the string x. So from now on, all my (new) scripting will be in Python. This includes, by the way, the Roman numeral clue page that’s just gone up. So there’s that, too.

Update: here’s Joon’s perl code to do the same thing.

#!/usr/bin/perl
use strict;

my %dict;
open DICT, "scrabble.txt";
while (<DICT>) { chomp; $dict{$_} = 1; }
close DICT;
for (sort keys %dict) { print "$_\n" if (/^..(.)\1.$/ && $dict{reverse($_)}); }

12 Comments

  1. Pingback: Anagram Word Ladders | Box Relatives

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.