Gwynne's Blog

Sa souvraya niende misain ye

Tic-Tacs No Comments

With apologies to Pete Seeger and Malvina Reynolds…

Little Tic-Tacs in the boxes
Little Tic-Tacs made of sugar gum
Little Tic-Tacs, little Tic-Tacs, little Tic-Tacs all the same
There’s a white one, and a white one, and a white one, and a white one
And they’re all made out of Ticky-Tacky
And they all look just the same

This came to me when someone said “Tic-Tacs” and “ticky-tacky” to me. That is all.

, ,
June 17, 2010 at 10:22 pm

The utility of a scripting language. No Comments

I feel like quite a geek. I had some text copied from my IRC client that I wanted to transform to XML for my XSLT sheet to display all nicely on the Web interface. Format of a line copied from the client:

altered nickname<tab><tab>message<tab>hh:mm:ss<space><AM or PM><carriage return>

Correctly formatted XML for the XSLT sheet:

<message><time>unix timestamp</time></time><type>2</type><sender>correct nickname</sender><content>message</content></message>

How to transform this? I could’ve done the majority of the work with a PCRE regexp and search/replace, but that wouldn’t have fixed the nicknames (since you can’t make if/else decisions in a replace in most editors) or calculated the correct UNIX timestamps. So I turned to scripting, of course. Some would have chosen to use Ruby, others Python, or Perl, or possibly even bash for some masochistic reason. I chose PHP.

Took five minutes, most of which was spent constructing the regexp. The code:

<?php

$conversation = file_get_contents(__FILE__, false, NULL, __COMPILER_HALT_OFFSET__);
$valid_nicks = "nick1|nick2|nick3|nick4|nick5";
preg_match_all('/^('.$valid_nicks.')(?:\t+)([^\n\t]+)(?:\t+)(\d+):(\d+):(\d+)[ ]([AP]M)$/mSu', $conversation, $matches, PREG_SET_ORDER);
$xml = "";
$time = time();
foreach ($matches as $splitline) {
    $nick = $splitline[1];
    $message = $splitline[2];
    $hour = $splitline[3];
    $minute = $splitline[4];
    $second = $splitline[5];
    $meridian = $splitline[6];
    if ($nick === 'nick1' || $nick === 'nick2') {
        $nick = 'real_nick1and2';
    } else if ($nick === 'nick3' || $nick === 'nick4') {
        $nick = 'real_nick3and4';
    }
    ++$time; //mktime($hour + ($meridian === 'PM' ? 12 : 0), $minute, $second, date('n'), $meridian === 'PM' ? 1 : 2, date('Y')));
    $xml .= "<message><time>{$time}</time><type>2</type><sender>{$nick}</sender><content>{$message}</content></message>\n";
}

echo $xml;

__halt_compiler();
// the conversation was pasted here

I daresay that was a pretty cheaply elegant bit of work, if I may be allowed to pat myself on the back. Entirely trivial stuff, but it shows how useful scripting can be for some tasks. How inane would that conversion have been, replacing the nicks by hand and calculating the timestamps one at a time? The conversation was about 500 lines long. Yay scripting.

Please, don’t comment with a one line Perl script to do the same thing from STDIN, I’m well aware you can use Perl to compress any complexity down to what looks like a couple hundred bps of line noise :-D.

P.S.: I am fully aware that the code has several inefficiencies, odd-seeming decisions, things that could’ve been done better, and so on, and so on. Who cares? It works. It’s not meant to win design awards.

, , , , , , , , ,
May 2, 2010 at 3:52 am

Me, Gwynne No Comments

Yes, I am a woman. And I’m 27. And I’m a programmer.

That is all.

“Anything that happens, happens. Anything that, in happening, causes something else to happen, causes something else to happen. Anything that, in happening, causes itself to happen again, happens again.

It doesn’t necessarily do it in chronological order, though.”
Mostly Harmless, Douglas Adams.

, , , , , , ,
February 7, 2010 at 10:56 pm

The last remnants of the old Republic have been swept away. No Comments

“Impossible! How will the Emperor maintain control without the bureaucracy?”
“The Regional governors will have direct control over their territories. Fear will keep the local systems in line; fear of this battle station.”

“Guinan, I need your help. Could you sit over there?” – Riker
“Seems simple enough…” – Guinan

, , , , , ,
September 4, 2009 at 8:29 am