Thursday, February 26, 2009

Another day at the office

I was at work.

In the dream, it seemed like I knew I was still working AMPS, but I don't remember thinking it explicitly. How does that work? Maybe I inferred it because the layout of the building was sort of the same except the walls and floors looked sparkling new. In real life, the building was on the demolition list, but they didn't have the funds to tear it down. "Let's move AMPS there!" said some kind soul. Believe it or not, it was a step up from being next door to the laundromat and auto shop.

So I'm at work standing in the hallway by Robert's office that leads outside to the loading area. I see my wife turn the corner off the main hallway. (The connecting hall was much longer in the dream than in the real building.) Soon after, this little skunk comes sauntering behind her. I don't use that as a cliché: it was grinning cartoonishly and had a happy-go-lucky bounce to its step.

I alerted my wife: "Sam!" This startled the skunk. I remember hoping it wouldn't spray, but it lowered its head, stuck its butt in the air, and shot a stream of blackish liquid in a nice upward arc just as an unknown black guy turned the corner. Poor guy: wrong place, wrong time.

Next thing I know, the little critter is chasing me, but I had a plan: I knew I was near a kitchen or break area with two doors close together in a corner — openings in both adjoining walls — and I was going to weave around through the doors to confuse the skunk. This would have been about where Bo's office or the bathroom was, but the real building has no such room.

So I do the weave thing and end up retracing my path down the hall that opened to my office, toward where I was when the dream started. Now a married couple Don and Karen were in the hall holding open large black garbage bags intending to catch the skunk. I ran past them (not sure how: the real hall is narrow), and the skunk ran into one of the bags. They tied up the bag, and Don said something about what to do with it — I can't remember what.

The guy who walked into the stink remarked that he'd been following the skunk, so it must have lost his scent. This made me think of a principle illustrated in an episode of Mr. Wizard's World where he blindfolded a kid, placed a bottle of vinegar under her nose, and told her to say when she thought he'd taken the bottle away. Even though the bottle was still under her nose, she thought he'd removed it. The technical name for this phenomenon is olfactory fatigue.

Monday, February 23, 2009

Scapegoats and the mortgage crisis

When my wife and I were shopping for houses a few years ago, an originator preapproved us for a loan amount so ridiculously high that I knew a mistake must have happened somewhere.

"But you have no debt," the agent insisted.

At this point, I could have patted myself on the back for having arrived, for being a success, for Being Somebody, for having Achieved The American Dream.

I'm sure the agent would have been quite pleased to bank the commission check on such a monster. My family, on the other hand, would have been so house-poor that developing a taste for licking paint off the walls would have become a necessity.

The agent was doing her job. She has zero responsibility for me or my family: that's my job. Had I plunged so deeply into debt, I would have been the fool. I'd have been even more foolish to do so willingly but then turn around and charge "Predatory Lending!"

The same thing happens on car lots. For most people, buying new cars is stupid, and leasing cars even worse. Wise people don't go to car lots seeking financial advice from car salesmen. For good reason: it's not their job! A car salesmen is there to sell you the car you want, regardless of what a lead weight around your neck it'll end up being and the enormous opportunity cost you'll pay.

That cute girl at the office whose marriage is on the rocks and likes to make flirty comments, you could easily throw away your own marriage and your own family over her. Keeping it in your pants is your job, not hers.

Opportunities to screw up royally are everywhere. Children and the incompetent must depend on others to prevent them from making stupid mistakes. It's part of being adults for the rest of us.

Wednesday, February 18, 2009

Scraping data from a program's output

Consider a (truncated) output from vmstat on AIX:
  2031616 memory pages
  1953185 lruable pages
   935166 free pages
        1 memory pools
   170943 pinned pages
     80.0 maxpin percentage
  ...
Say you want to grab the values for memory pages and free pages. Below, I explain a couple of ways to do it.
#! /usr/bin/perl

use warnings;
use strict;

no warnings "exec";

open my $fh, "vmstat -v |"
  or die "$0: can't execute vmstat: $!\n";

my %vmstat;

while () {
  chomp;
  my($n,$desc) = split " ", $_, 2;

  $vmstat{$desc} = $n;
}

print "Memory pages: $vmstat{'memory pages'}\n",
      "Free pages:   $vmstat{'free pages'}\n";
When the filename argument to open ends with a pipe, Perl runs the named command and makes its output available on the returned filehandle. I turn off the autogenerated error (no warnings "exec") because I like my format better.

Looking at vmstat's output, each line has a value and a description, so the plan is to read each line and stash the parameters where we can find them later. A hash is a perfect data structure for this task.

Most of the time, the pattern to the split operator is a regular expression, but with no arguments (or a pattern of a lone space) it acts like awk, throwing away leading whitespace. Because the descriptions contain spaces, we don't want to split on them and tell Perl to give us back exactly two fields. Because we've limited the number of splits, we have to remove the trailing newline with chomp.

The output is straightforward: print the desired values.

You can of course be more clever:

#! /usr/bin/perl

%vmstat = reverse `vmstat -v` =~ /(\S+) (.+)/g;

print "Memory pages: $vmstat{'memory pages'}\n",
      "Free pages:   $vmstat{'free pages'}\n";
Instead of a piped open, this time we use backticks (``) to capture vmstat's output and from the output extract the values and descriptions.

The regular expression \S+ means a sequence of one or more non-whitespace characters, and this matches the numbers in the output. You might be tempted to use \d+ (one or more digits), but this will give you surprising results on the floating-point numbers.

By default, dot does not match newline, so the (.+) subpattern matches through the rest of the current line — the description in this case.

The /g regular-expression switch means we get all possible non-overlapping matches.

The list returned from the match will look like (2031616, "memory pages", 1953185, "lruable pages", ...), but that's the opposite order from hash initialization, i.e., key then value. The reverse operator fixes this problem.

Friday, February 13, 2009

25 random songs

Bo hit me with the 25 random songs meme. Who wants to push buttons when you can write a program?
#! /usr/bin/perl

use warnings;
use strict;

use File::Find;

use constant CHOOSE_N => 25;

@ARGV = "." unless @ARGV;

my @mp3;
my $matches = sub {
  push @mp3 => $File::Find::name
    if /\.mp3$/i;
};

find $matches => @ARGV;

# Fisher-Yates-Knuth
for (my $n = $#mp3; $n >= 1; $n--) {
  my $k = int rand($n+1);
  @mp3[$k,$n] = @mp3[$n,$k];
}

for (1 .. CHOOSE_N) {
  last unless @mp3;
  my $song = shift @mp3;

  # e.g., .../Music/Bob Marley/Kaya/08 - Crisis.mp3
  if ($song =~ m!^.*/([^/]+)/[^/]+/\d+\s+-\s+(.+)\.!) {
    my($artist,$title) = ($1,$2);
    print qq{$_. "$title," $artist\n};
  }
}

The output:
  1. "The End," The Doors
  2. "Misty Morning," Bob Marley
  3. "My Mood Swings," Elvis Costello
  4. "Concerto Emperor: II. Adagio un poco moto," Ludwig van Beethoven
  5. "What's the Matter Here?," 10,000 Maniacs
  6. "Trench Town Rock," Bob Marley
  7. "Final Hour," Lauryn Hill
  8. "These Are Days," 10,000 Maniacs
  9. "Want-Ad Blues," John Lee Hooker
  10. "Loser," Beck
  11. "Sonata Pathétique: II. Adagio cantabile" Ludwig van Beethoven
  12. "Peer Gynt Suite No. 2: Ingrid's Lament," Edvard Grieg
  13. "4 Better or 4 Worse (interlude)," The Pharcyde
  14. "Bonita Applebum," A Tribe Called Quest
  15. "Hard Hearted Woman," John Lee Hooker
  16. "Jeremy," Pearl Jam
  17. "Toccata and Fugue in D minor," Johann Sebastian Bach
  18. "Ya Mama," The Pharcyde
  19. "Pennyroyal Tea," Nirvana
  20. "Concerto in C minor for Violin and Oboe," Johann Sebastian Bach
  21. "Sex Type Thing," Stone Temple Pilots
  22. "Drive," R.E.M.
  23. "People Are Strange," The Doors
  24. "Vivrant Thing - Violator (feat. Q-Tip)," A Tribe Called Quest
  25. "The New Pollution," Beck

Oh Lucy, I'm hooome!

I got home from work and was telling a grandmotherly figure about my day. I said something about an issue with a wiki, and she said it was important to get the thing fixed.

Thinking back, I don't recognize the layout of the house, and I don't recognize the Latino woman who spoke with an accent. I knew her in the dream.

There was some issue about a check she had given at church that day. I assumed she had been to Mass because it was a workday, and she was probably Mexican. (I am not Roman Catholic.) She was on the phone trying to call a woman at her church about it when I got home. She was frazzled because of this and because of a baby somewhere in the house who was calling out unintelligibly. A few times, she raised her voice to be heard and to say she'd be there soon.

As I left the front of the house, I passed by a kitchen on the right where a Latino guy about my age was doing something. Again, I knew him in the dream, but I'm not sure what our relationship was. I thought about saying "Buenos dias" but didn't because I wasn't sure whether the greeting was linguistically appropriate.

Making two left turns, I got to what I assume was my bedroom. I think I emptied my pockets there but don't remember specific items. I could still hear the baby calling, but being closer didn't make it sound any less odd.

I go into the room in the back of the house on the right where the baby was, and he was barely floating face-up in an inflatable pool on the bed. "Oh my God! C----!" I shouted. I did recognize the baby: he was an infantized version, but a big baby, of my friends' son. He was bobbing and made his weird noise when his mouth surfaced. I ran over to him. His face was smurf-blue but not all over, as though his part of his face had been dipped in paint.

I sat him up, and he threw up a couple of times in the water. First a little bit and then a thin-milkshake gusher.

That's when I woke up. As I sat reflecting, I could still hear the baby's noise: something was partially obstructing one of my nostrils, making a faint whistle-wheeze.

Wednesday, February 11, 2009

Playing chess with Maddy

My four-year-old daughter wanted to play against the computer in Chess Titans. Observations:
  • She plays an unorthodox opener: p-h4 and then continuing right-to-left, she advanced pawns one or two spaces (apparently at random).
  • No concept of position.
  • Her understanding of material advantage is better suited for checkers: a few times, she asked, "Do I have more of their people?"
  • Her strategy is better suited for a first-person shooter. A couple of times, I tried to explain that she was making bad trades, but the appeal of capturing was irresistible.
  • Knights on d2 and e2 were highly comforting.
Still, it's a start!

Thursday, February 05, 2009

Kids say the darndest things

This morning on the way to school, my 8-year-old son and I were talking about his spelling words. One was investor, and I asked whether he knew what it meant.

"Is it someone who invents something?"

"No, that's an inventor. Hmm, let's see. Do you know what profit is?"

"Like the guy in Halo?"