Luhn algorithm in Perl

Here’s an implementation of the Luhn algorithm in perl.

#!/usr/bin/perl
use strict;
my $number = shift(@ARGV) || die("Usage: $0 <number>\n");
$number=~s,[^0-9],,g;
my($sum,$odd);
foreach my $n (split(//,$number)) {
        $odd=!$odd;
        if($odd) {
                $sum+=$n;
        } else {
                my $x=2*$n;
                $sum+=$x>9?$x-9:$x;
        }
}
my $ok = 0+(($sum%10)==0);
exit(($sum%10)!=0);

(Download)

This program was designed for shell scripting, using something like

$ if ./luhn 457623486; then echo "ok"; else echo "error"; fi
ok

but it should be trivial to modify it for other purposes.
I’m using this algorithm to validate GSM IMEI numbers, but the Luhn algorithm is also behind credit card numbers.

Phonetize

A small filter to phonetize STDIN into the NATO phonetic alphabet. It’s very useful to spell passwords over the phone.

#!/usr/bin/perl
use strict;
my %ALPHA = map {uc(substr($_,0,1))=>$_} qw( Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu );
while(<STDIN>) {
        chomp;
        print $_, "\n";
        print join(' ',map {$ALPHA{uc($_)}||$_} (split(//,$_))),"\n";
}

Here’s the source code.
Yup, I know about Lingua::Alphabet::Phonetic::NATO but I needed a quick script without module dependencies. Yes, I haven’t been able to learn the NATO phonetic alphabet yet -unlike some geeks with too much time in their hands (you know who you are)- and yes, I know I’m lazy, but that’s why I am a perl fan anyway ;-) .
There’s more information about this alphabet atWikipedia. You migth want to donate a few bucks while you’re there.

Los Sangre are coming for you

mind-on-fearJust in time for Halloween. Last Thursday I didn’t get my fix of morning radio, so this note in cofradia.org came as something of a surprise. “Beware. Los Sangre are coming for you”. The baloney detection device went off immediately, and a quick visit to snopes.com confirmed this as a well-documented urban legend. That didn’t stop the media from spreading the word -the rumour was legitimized by an official press release, after all- and further questioning to other officials in the federal and state level confirmed the presence of the gang in the country. “We are taking it seriously” sounds good enough. After all, what government spokesperson is willing to admit that he doesn’t have a clue about whatever the hell you’re asking him about?. It doesn’t hurt to say that “the (National Police Force) is on alert” and that “(the Federal Government) is informed about the situtation”, does it?
It’s not the first time that the general insecurity in Mexico contributes to give some plausability to an incredible story, and it certainly won’t be the last. Here lawlessness defies imagination and impunity is taked for granted. Take into account that criminals can break into a government facility and take back the smuggled merchandise that’s been impounded from them, and then the rumour of a macabre gang initiation game may start to seem plausible. In a country where crime statistics makeup and official coverups are routine, if some random government official tells me to be on the lookout for a specific gang ritual, he may have a good reason for it. We forget that the work of the government agencies -what *we* actually pay them for- is to give us security, not to blow out of proportion whatever chain mail “alert” falls into their inbox. I guess that’s criminal investigation in the third world amounts to. What’s next? A press release advising the population to be on the lookout for AIDS pin pricks in movie theaters?. “Hey, it may or may not be true, but we are alert just in case. You have been warned.”. I guess that this way nobody can blame them if quantum probabilities reverse unexpectedly and the rumour just happens to be true. Heck, even a false sense of security is better than having The Government issuing bogus alerts that send the population into panic. Understandably, the population is totally psyched out. Since the story has been all over the radio and TV, everyone from laymen to well-studied intelligent people is behaving in a totally unrational way. This is your primal mind on fear. That’s what panic does for you.
Now some media outlets are waking up to the fact that this is an urban legend that’s been repeating for some time in different countries and there have been mentions of Snopes in a couple of articles. However, the undertone in some of them is unnerving: “American site dismisses a very real threat”, is my lecture of some of them. Hey, these guys should know better. Next time someone refuses to compare bloggers with journalists I might be tempted to take it as a compliment.
Now why would a government official issue a press release without further research? Because the alert came from the Interpol office in a nighbourging country, that’s why (by the way, Guatemala officials show a healty skepticism) and because the head of the state government “got the alert in his computer”. I’d really like to know *who* jumped the gun, because so far I haven’t been able to find a copy of the press release that started it all, the initial vector for this particular outbreak, although some stories point to some “DGA-PII/1548/2005” document. We better find it quickly, because once the situation comes out as it is nobody is going to take the blame. Never mind the racial undertones. The fact that officials in other countries have fell for this urban legend before doesn’t excuse ours from crying wolf over a lie, and whoever is responsible should be removed from office at once (*Ha!*).
Now for the final outcome I can only imagine what the justification will be for falling for an obviously bogus chain mail. Already, some officials have justified the heihgtened security alert by saying that they’re trying to stop copycat killers or outright pranksters. Some will say that this was an obvious cartel-backed maneuver masterminded to distract the police, or some form of guerilla warfare. Some will even talk about cyberterrorism. Or blame the media. Or Interpol. Or Guatemala. Or play semantics. I bet that others will say that this might be a conspiracy to destabilize the country — as if we needed further help with that. I’ll just stick to a corollary of Ockham’s razor in this: Never attribute to malice that which is adequately explained by stupidity.

Search for victims of Hurricane Stan

The government of the State of Chiapas has recently published on the web a list of the shelter where each victim of Hurricane Stan is right now, so their relatives can find out where they are and how to get in touch with them.
It is a good idea and undoubtel it was done in a hurry, but it is poorly implemented: the list is published in two formats: a 1.2MB PDF file and a 5.6MB HTML file (in the site the file size is listed at22MB!) that were exported directly from Microsoft Excel. If you are looking for a relative whose fate remains uncertain, it is kind of heartless to force you to download a 8MB PDF Viewer or a 5.6MB file to even start looking for your loved ones. Even more if the server doesn’t support content negotiation for GZIP compression, which would shrink the file to 262KB — under 5% of the original size! (bzip2 compression is even more efficient, the compressed file would be half that size). I will spare you of the rant about Microsoft’s disgusting HTML format.
Well, I downloaded the file and wrote a small perl script to clean up that file and import the records into a SQL database. It even does Soundex translation for improved accuracy thanks to Text::Soundex. Perl is Beautiful. If you can, you may want to publish that database on your own site to give people an opportunity to find their relatives, or point them to navegando.net where the search will be kept indifinitely and the database will be refreshed as needed.
BTW, my relatives reported while Stan was still in full force and all of them turned out unharmed.

Changing TiVo’s timezone and date/time

To change your TiVo’s timezone and date/time follow these steps:

  1. Get timezoneadj30.tcl. I don’t have access to tivo_canada’s archive, so I had to write my own timezoneadj30.tcl.
  2. Edit it and change the timezone. It seems that 0=GMT,3=EST
  3. Run ./timezoneadj30.tcl
    # ./timezoneadj30.tcl
    
  4. Set the clock using settime, e.g.
    # settime 20031123025900
    # settime -rtc
    
  5. I noticed that if you set the time back, the IR blaster stops working. I’m not proud of my solution:
    # reboot
    

Note: This post was originally listed under the static TiVO page, but I’ll start managing the TiVo section with WordPress.

How to make iTunes 5 ignore the Windows locale

About iTunes 5.0.1.4 I recently upgraded iTunes to 5.0 on Magda’s laptop and it decided to start speaking to me in Spanish. In my Thinkpad with Windows XP in english iTunes was still in english.
Magda’s laptop came pre-installed with Windows XP in spanish, but I have never had a problem installing an english version of any kind of software in it. So far, every iTunes version had asked for an installation language during setup, and stuck with that language forever. iTunes 5.0 is different: no matter what language you use during setup, it will move on to the system locale whenever you run it. I bet that somebody in Cupertino believes that this is a “feature”, but actually iTunes is ignoring an explicit user setting, a usability do-not that comes as a surprise from the UI gurus at Apple.
Just to state a fact, I’m no Malinche. I write and speak an above-average Spanish, and I do love my country and its language. But I like my english software in English, and I do not have to endure the work of some underpaid english-to-spanish translator that can’t speak either language right nor understand their respective subtleties. Besides that, the iTunes ES translation is not for Spanish but rather for Castellano, and some strings are somewhat annoying. “Party Shuffle” sounds nice. It has a festive tone to it. When iTunes is in Party Shuffle mode my subconscious thinks I’m at a party, even if it’s 2am in the morning with two deadlines to meet early in the day and a senior programmer on vacation. In comparison “Sesión aleatoria” -random session- sounds just plain boring.
Today I upgraded to 5.0.1.4 half expecting this bug to be gone, but surprisingly it is alive and well and sends best wishes to everyone back home.
A quick google search got back an interesting result on Playlist Magazine’s forums: just close iTunes, go to its installation directory under “Program Files”, find every directory named <offending language code>.lproj (for me that was es.lproj) under iTunes.Resources, iTunesHelper.Resources and iTunesMiniPlayer.Resources and nuke them out of existence. The one under iTunesHelper.Resources might complain about a DLL being in use. Just move that DLL to some other place and delete it after a reboot. The next time you run iTunes it will talk to you in English.