Wednesday, August 30, 2006

Perl one-liner: sort a string of numbers

This is a stupid pet trick with Perl.

I was recently writing an email to a cow-orker and had a long list of numbers to put in the email. I had them like this:

56165, 55163, 56137, 56104, 56115, 55108, 56971, 55768, 56682, 56585, 55940

My cow-orker likes things to line up and be in order, so I needed to sort them. I reckoned I could use split, sort, and join in Perl to make this job easy. It worked. Here is the one-liner I came up with:

perl -e "@_=split(/, /,'56165, 55163, 56137, 56104, 56115, 55108, 569
71, 55768, 56682, 56585, 55940');print join(', ',(sort @_))"

55108, 55163, 55768, 55940, 56104, 56115, 56137, 56165, 56585, 56682, 56971


Monday, August 28, 2006

Hate 2: City Face

A few weeks back, I talked about how I got yelled at and threatened by a crazy guy on the street here in Philadelphia (Hate). This same thing happened to me again a week ago. Another crazy homeless guy yelled at me. I tried to play it off, but it kind of got to me. The first time it could have been a chance thing. But twice in a month? Maybe I was provoking it somehow.

I asked a native Philadelphian about it, and he told me to stop smiling in the street. I needed to adopt a "city face", not smiling at strangers, not looking at strangers.

It sounds awful, but I've been trying it out. The fact is that I understand why city folk walk around with zero expression on their faces. No one messes with me now. Homeless guys don't bother asking for change.

It has its downside-- I walked right past a friend without seeing him, for example. On the other hand, I understand that there are a lot of people out there who just aren't right.

Thursday, August 24, 2006

Microsoft SQL Server: how to truncate the ERRORLOG

The Microsoft SQL Server error log is stored at c:/Program Files/Microsoft SQL Server/MSSQL/LOG/ERRORLOG (or similar). This can get very big, because it only recycles when the instance is restarted. When it's too big, it becomes difficult to parse it for errors.

When the instance restarts, the ERRORLOG is renamed to ERRORLOG.1 and a new ERRORLOG is started. As this hapens again and again, the number of the file increases.

You can force the ERRORLOG to recycle without restarting the instance with this simple command:

osql -S myserver -E -Q "exec sp_cycle_errorlog"

Or, of course, you can run the command "exec sp_cycle_errorlog" from Query Analyzer.

Microsoft SQL Server: setting your editor in oSQL

This is another oSQL trick. I like to use gvim as my text editor. It reinforces the fact that I am a dinosaur. And it has great regular-expression-based pattern matching.

There was a command to set the editor in SQL*Plus, so I wantetd to be able to do the same in oSQL. The default editor in oSQL is the program "edit. You invoke it from within oSQL by typing "ed".

You set the editor in oSQL by setting the EDITOR command line variable. Here is how I set mine:

set EDITOR=gvim -c "set ft=sql"

This also calls the VIM command to set the syntax to SQL, which gives you pretty color-coding and such.

This tip could be adapted to UltraEdit and similar.

Microsoft SQL Server: how to run a script from within oSQL

I like to use oSQL to do a lot of my work in SQL Server. I'm used to using SQL*Plus in Oracle, so I'm comfortable with the command line. Also, it reassures me to know that I can do my job even if I only have a command line to work with.

There are a few things I liked about SQL*Plus that I had not found in oSQL until recently. One was the ability to run a script from within an oSQL session. You do this with the :r command:

c:>osql -S myserver -d mydb -E
1>:r my_script.sql
2> go
... The script runs ...
1> exit

You could have run this all from the command-line one time through like this:

c:> osql -S my_server -d my_db -E -n -i my_script.sql

However, the :r command lets you read in scripts within an already open session.

Tuesday, August 22, 2006

1:02:33 - BF Parkway past boathouse row (6.3 miles) -

It was a pretty day, so although I was pretty beat from Bikram this morning, I decided to run. I ended up clocking just tover an hour. Currently I have Italian lessons loaded on my MP3 from I really like LearnItalianPod. They are an Italian woman and man that do a simple dialogue in each PodCast. Their lessons are about 10 minutes each, which are enought that I can listen to a couple when I walk to and from work. They seem pretty simple-- of course, I can't vouch for that, because learning Italian when you already have pretty good Spanish is really simple. It's not exactly the same (and my Italian co-worker tells me it's really annoying to listen to a Spaniard who thinks they *are* the same). But some essential stuff is in both-- gender matches in most cases, and probably the most important: 17 shared verb forms. So once you've got your brain around the Past Subjunctive in Spanish, you don't have far to go to learn it in Italian. If you've been reading my blog for a while, you may remember that I was studying Mandarin for a while. I haven't abandoned that, but I was getting a little burnt out on it. Italian's great because I'm already somewhat conversent in it after just a few weeks (I talked for quite a while with some Italian folks in San Diego after the ESRI conference). It would have taken years to get that far in Mandarin.

Monday, August 7, 2006

Using the Perl Xbase module to read xBase databases

I recently had occasion to read some xBase database files (e.g. dBase or FoxPro files). What I was doing needed some of the capabilities of Perl. I found a module that reads xBase databases directly: Xbase.

Here is a simple set of code to read all of the records in an xBase database:

use strict;
use Xbase;

# Create a new Xbase object
my $database = new Xbase;

# Open the database
my $db_name = 'C:exampledatabase.dbf';

# Get the last record number
my $end=$database->lastrec;

# Go to the first record

# Loop through the records
for my $i (1..$end)
# Print the record number
print $database->recno . ". ";

# Get the fields in this record
my @fields = $database->get_record;

# Loop through the fields
for my $f (0..$#fields)
# Print the values of each field
print $fields[$f] . "\t";
print "\n";

# Go to the next record

# Print information about the database,
# including the names of the fields
print $database->dbf_type . "\n";
print "Last update: " . $database->last_update . "\n";
print $database->dbf_stat;

# Close the database

This code is adapted from the perldoc documentation of the Xbase module.