Figuring
Phone-y Words
Randal L. Schwartz
Every day, we seem barraged with phone numbers that spell things,
like please dial 1-ZZZ-HE-MUST-PAY to force your older brother to pay
for the call!. That's because since nearly day one of dial phone service
(back when it was really a dial), we've had these letters that go along with
each of the digits.
On one of the many mailing lists I follow, a question came up the other day:
someone wanted to know if there were any large sets of words that all collided
to the same number. I thought that'd be a perfect job for Perl, and in a short
time, came up with a quick program to scan through the standard UNIX dictionary
in /usr/dict/words to find the longest such collision list (or lists,
if more than one). Since the program illustrates some basic data reduction techniques,
I thought I'd pass it along to you as well.
First, comes the most critical part. Given an arbitrary string, like merlyn,
what are the digits used to construct that? Well, we need to know that m
is 6, and e is 3, and so on. Now, there are many slow ways to do this,
but the fastest way is a tr operator:
$_ = "merlyn";
tr[abcdefghijklmnoprstuvwxy]
[222333444555666777888999];
print;
which prints 637596. Here, I've used the feature that permits tr operands
to be delimited with arbitrary balancing punctuation, together with the optional
whitespace that can be between the old list and the new list when we use such
delimiters. This gives a nice visual layout to let me verify that I've got the
right characters with the right translation.
|