Monthly Archives: August 2007

Aftermath

Aftermath.jpg

The only word for last night: Quagmire.

[Update: 8/26/07 16:15]: “You have to wait here. We’re sending the cars up one-by-one when they say they’re ready. We’re going to have you get up some speed, and do not stop until you get completely into your parking space. They’re already getting tired of pushing…”

Not going anywhere for a while…

RATM.jpg

So last summer I got stranded in Auburn, WA after the Goo Dolls – Counting Crows concert and needed my roomate to come rescue me. I thought that was the longest it had ever taken me to get home from a concert. Until Rage Against the Machine at Alpine Valley, that is. We are now waiting in what can only be described as a mud pond. Picture about 40,000 people trying to move their cars out of a parking lot. Now replace the parking lot with a flooded field. And make all the people drunk. That’s about what we’re at… Oh, and the cars around us haven’t even moved in like 40 minutes. 🙂 See you in the morning?

Playing with PERL

Warning: The following content is of a ridiculously nerdy nature, and probably unsuited for most of the viewing audience. That said, I had a lot of fun writing it, so here it is 🙂

My roommate Dave IMed me the other day with a problem: He needed a program in 30 minutes that would search through a text file for any occurrences of a list of CAPITALIZED words, and convert them to lowercase, wherever they occurred.

I received his message 20 minutes later, set to work, and in the last 9 minutes, developed a script for him, along with an extensive test case. PERL, of course, was born to solve this problem.

Here’s the the test case I made up, and the correct output, to get an idea of what I needed to do:

input.txt

There once was a Chicken named EGgS.
It lived STRINGS in a barn.
The CHICKEN was afraid of EGGSNITCHERS.
Chicken likes Eggs served on STRINGS

output.txt

There once was a chicken named eggs.
It lived strings in a barn.
The chicken was afraid of EGGSNITCHERS.
chicken likes eggs served on strings

(I later realized this missed one rather important case… BUG: see if you can figure out what it is, and what the error in the first three drafts below is)

Here’s the first draft, which works (nearly…see bug note above) correctly and was submitted within the prescribed 30 minutes :-):

munge1.pl

#!/usr/bin/env perl
use strict;
my @patterns = (
"chicken",
"eggS",
"strings"
);

# Make sure user used lowercase
map { tr/[A-Z]/[a-z]/; } @patterns;

my $input_file = $ARGV[0] or die "Usage: go.pl n";
open (FH, $input_file) or die "Could not read from file: $input_filen";

while (my $line = ) {
foreach (@patterns) {
$line =~ s/^$_(W)/$_$1/i;
$line =~ s/(W)$_$/$1$_/i;
$line =~ s/(W)$_(W)/$1$_$2/i;
}
print $line;
}

close FH;

exit 0;

But then I thought to myself… “Self, this is PERL. Surely there is a shorter way?”
Removing some “useless” error-checking and file parsing code in favor of a shell-out, I came up with this:

munge2.pl

#!/usr/bin/env perl
my @patterns = (
"chicken",
"eggS",
"strings"
);

map { tr/[A-Z]/[a-z]/; } @patterns;

map {
foreach $a (@patterns) {
s/^$a(W)/$a$1/i;
s/(W)$a$/$1$a/i;
s/(W)$a(W)/$1$a$2/i;
}
print;
} `cat $ARGV[0]`;

Better, shorter, PERL-ier 🙂

But still not really PERL. I mean, come on. There were 3 entire statements there. Laaame.

So I played a bit, and moved the first map around to compact two statements into one (admittedly, the map is just there to make sure the user’s list of TERMS to lowercase is *actually* lowercase, but I wanted to keep that bit of functionality):

munge3.pl

map { tr/[A-Z]/[a-z]/; } (@patterns =  ("chicken","eggS","strings"));
map { foreach $a (@patterns) { s/^$a(W)/$a$1/i; s/(W)$a$/$1$a/i; s/(W)$a(W)/$1$a$2/i; } print; } `cat $ARGV[0]`;

Nice. But still not PERL-y. 😉
Then it hit me: why am I wasting an entire statement to create an array that I will only use in one other statement? Oh, and while we’re at it, let’s cut those 3 regexps down to 1, courtesy of a good insight from Jason. (Use b and B to match word boundaries.) AND, in a throw-back to my early days of escaping URL strings in CGI (like: s/(W)/sprintf("%%%02x",ord($1))/eg) let’s move the lowercase-ifying inside the regexp as well, eliminating the first map altogether.

munge.pl

map { foreach $b("chicken","eggS","strings"){s/b$bB/lc $b/ieg;} print;} `cat $ARGV[0]`;

Ahhh… that is PERL :-). One line of file-munging goodness. Use only as directed:

/usr/bin/env perl munge.pl input.txt > output.txt

Note: I know this is not good coding practice, it was just fun to reduce to as short of a program as possible. And there’s something to be said for brevity, as well. (… is the soul of wit…)

If you’re concerned about the error-checking of said code, tack this on the end 😀

or die “Caught teh 3rrorz!!1”; # 😉

So that's why my legs are sore

22-mile Ride

Click the image to link to the full Google map. It appears that I biked 18.9 miles yesterday. (I walked from work to the movie theater with Jeremy, 3.0 miles worth of that map). I’m impressed. Hopefully this will serve as motivation to keep it up during the school year.

Route: Home -> work -> UPS depot -> home -> work -> movie theater -> home.

Dinner and a Movie

So Jeremy said Bourne Ultimatum was “explosionary.” A good word. I should probably see the movie.

Fast forward through some planning

Jeremy’s friend heard a rumor that you could bring your own food into the AMC theater.

We called the theater while walking there to try and verify.
They said “We’ve never denied entry to anyone with food.”
We were still uncertain, because this seemed to good to be true.

We left 60 minutes before the show, for what turned out to be a 50-minute walk.

To fit supper in there, we stopped at the In-N-Out.
Jeremy waited for the orders while I bought tickets.

We walked into the theater bearing two fastfood bags and sodas.
Nobody blinked an eye.

I watched the movie while eating a hamburger.
I paid less for my entire meal than the guy next to me paid for his bag of popcorn.
Maybe that’s not a big deal to some of you reading this who grew up used to that, but for a Wisconsin boy, that’s “woah”.

Oh, and the movie? Bourne Ultimatum? Go watch it. End of story.
Wait, wait, wait. Actually, watch Bourne Supremacy first. Then go watch it. Today.