16 October 2009

Oracle numeric test

A colleague and I found ourselves needing to write a one-off bit of SQL that used a character column in a join condition. The character column is performing double duty, sometimes acting as a (numeric) foreign key, and sometimes holding other data.

I was perplexed by the lack of some kind of "is numeric" test in Oracle's dialect of SQL. I scrounged around forums and found something that I thought would work, but my colleague finally put me straight and we used this condition:


REGEXP_LIKE(char_column, '^[[:digit:]]+$')

03 October 2009

News from Montevideo

The Economist reports on the success of the XO laptops in Uruguay. Interim results: mixed to negative.
In Escuela 95, up to half of the students in some classes have broken their machines, usually by cracking the screen or snapping the antennae that pick up a Wi-Fi signal.

28 September 2009

All that is the case

I'm working on a short piece of code to capitalize all the initials in a given sentence (headlines for news stories from a wire service, to be specific), in order to match house style. At first, I was a little surprised that I couldn't find a Java class to do most of the work for me. But once I waded into the actual sentences that I had to process, with their variations and exceptions, I came to the conclusion that some degree of RYO was called for. Here's my current draft:



import java.util.regex.*;

* * *

private String capitalizeAllInitials(String text)
{
//capitalize the first letter of every word in the passed text
//(including little words like "a," "the," "and," "to")
//also capitalize words in quoted and hyphenated phrases

//NOTES: The pattern requires a leading space; I have found that
//ingested stories already capitalize the first word of the title.

Pattern p = Pattern.compile("(-|( (`|\\\"|\\\')?))([a-z])");
Matcher m = p.matcher(text);

StringBuffer sb = new StringBuffer();
while (m.find())
{
m.appendReplacement(sb, m.group(1) + m.group(4).toUpperCase());
}
m.appendTail(sb);

return sb.toString();
}



As the comments note, this code will handle ordinary words (The quick brown fox jumps over the lazy dog becomes The Quick Brown Fox Jumps Over The Lazy Dog), hyphenated phrases (Senator proposes pay-as-you-go plan becomes Senator Proposes Pay-As-You-Go Plan), and quoted phrases (Accused confesses, "we did it" becomes Accused Confesses, "We Did It") with single, double, or backquotes. The code assumes that the first word is already capitalized, so if that's not the case with you, you would need to add a ^ to the regular expression.

This code also doesn't handle the common forms of title casing, whereby articles, prepositions, and other small words are not capitalized. Also, this code capitalizes proper names and trademarks indiscriminately.

20 September 2009

Respect the pomodoro

With Patricia R. Olsen, Jim Remsik writes a good first-person account of pair programming as it is practiced at Hashrocket:
When senior and junior developers are paired, junior programmers might feel intimidated. If this happens, the junior programmer might be asked to start as the driver, which may encourage the senior person to become a better teacher. It’s also a way to bring junior programmers up to speed quickly, because they benefit from the more senior employee’s knowledge.

15 September 2009

We used to call that a dirt road

Via RISKS Digest: complaining of poor bandwidth through Telekom (South Afica's leading ISP), an information technology company strapped a data card to the leg of a carrier pigeon, sent it flying 80 kilometers, and exceeded internet throughput by a factor of 50.