by simon baird

Friday, July 28, 2006

Aw... my first real inject

It's a special moment for any ruby programmer when they create their very first real inject. I don't mean in some sample code but in a real live program. It just happened to me and I'm very proud. :) Here it is:
#!/usr/bin/ruby

require 'uri'
require 'net/http'

LINE_MATCH       = /\/\/DATA\|\|\|/
FIELD_DELIM      = /\|\|\|/
NAME_VALUE_DELIM = /:/

url = 'http://www.jcu.edu.au/app/timetable/?subjectslist=CP1010&campus=Townsville&sem=2&doit=yes'

data = Net::HTTP.get(URI.parse(url)).to_a.grep(LINE_MATCH).map do |line|
  line.strip!.sub!(LINE_MATCH,'').split(FIELD_DELIM).inject({}) do |hash,pair|
    name,val = pair.split(NAME_VALUE_DELIM)
    hash.merge(name.to_sym => val)
  end
end

p data
I'm writing a hack to scrape timetable data from an existing web app and convert it to iCalendar format. I have the ruby iCalendar gem so I think I'm almost done. ...happy dance...

Wednesday, July 26, 2006

gpokr.com

Speaking of poker, this is cool. More info here.

Poker Night 3

The night went on later than previous poker nights. The third game finished about 2am. First game was won by me (the best one to win since it had the most players), second by my brother Nathan, (visiting from Sydney), third by my brother Daniel. Some may claim there was some family collusion going on. To this I point out that Nathan knocked me out in the second game (pair of kings vs pair of queens) and I knocked him out in the third (flukey straight on the river vs two pair). Despite some email sledging neither previous winner Michelle or Alex C made it to the winners circle, though Alex came second twice (second wins your entry fee back) and took home two bags of salty plums, so he wasn't too far out of pocket. Michelle had some bad luck when she bet hard on a four card "straight". Oops! Derek picked up a second place against Nathan, and Alex S was chip leader for a while in the final three on the last game, but she couldn't close the deal. Losers enjoyed a game of (erotic) 31 Bustop. We now know the rules governing when you can re-raise and also are getting a bit better at handling side pot confusion. So all-in-all a good time was had by all and blah blah etc. Next poker night some time in September maybe.

Tuesday, July 25, 2006

Hail Google Overlords

I just tried Gmail Mobile. I read some email and sent a message from my phone. It's pretty cool. The page says you can view attachments like pdfs, images and word docs on your phone. Wow. I wonder if I can send a photo as an attachment.

Update:
No sending attachments. I tried viewing pdf and word doc. Works nicely.

Monday, July 24, 2006

zune, regina spektor

Microsoft is going to launch an iPod competitor. It's called Zune. More here. But I really just wanted to show you the little cartoon and music here. The sweet tune caught my attention. It is Regina Spektor. Conan likes her. So does Julian Casablancas, so she must be good. Hang on a sec... Try this. Note to self, still haven't found any Françoiz Breut. She doesn't exist on iTunes, YouTube or my usual torrent fetchers. Might have to (gasp) purchase a cd...

Friday, July 21, 2006

The wonderful expressivity of why the lucky stiff

Oh rapture! Oh, delicious zesty snacks! Oh, steamed uncles in a cabbage display! OH, LAMB MEDALLIONS!

If you like that try some more of why's prose.

Thursday, July 20, 2006

Oracle PL/SQL function to split strings into tokens

I was trying to find how to split or tokenise (tokenize) a string in Oracle SQL. Technically you can do it directly with just instr and substr but your statement becomes very long and hard to read, mainly because you can't reuse the result of an instr. I found this which was interesting but not what I needed, so I wrote my own function to do the job. I'll post it here in the hope that it will be useful to someone.
create or replace function get_token(
   the_list  varchar2,
   the_index number,
   delim     varchar2 := ','
)
   return    varchar2
is
   start_pos number;
   end_pos   number;
begin
   if the_index = 1 then
       start_pos := 1;
   else
       start_pos := instr(the_list, delim, 1, the_index - 1);
       if start_pos = 0 then
           return null;
       else
           start_pos := start_pos + length(delim);
       end if;
   end if;

   end_pos := instr(the_list, delim, start_pos, 1);

   if end_pos = 0 then
       return substr(the_list, start_pos);
   else
       return substr(the_list, start_pos, end_pos - start_pos);
   end if;

end get_token;
/

Example usage:
select
   get_token('foo,bar,baz',1), -- 'foo'
   get_token('foo,bar,baz',3), -- 'baz'
   --
   get_token('a,,b',2),        -- '' (null)
   get_token('a,,b',3),        -- 'b'
   --
   get_token('a|b|c',2,'|'),   -- 'b'
   get_token('a|b|c',4,'|')    -- '' (null)
from
   dual
/

Notes
  • Remember that the index starts at one not zero just like strings in PL/SQL.
  • Empty tokens are counted. You get null if you ask for an empty token.
  • You also get null if you ask for an index greater than the number of tokens.
  • The delimiter defaults to a comma but you can specify any delimiter. A delimiter of more than one char should work also.

Update 22/8/12

When I wrote this I didn't know about regexp_substr. (Or maybe the version of Oracle I was using didn't have it). You can do this much easier using the regexp_substr method. Have a look at the regexp_substr docs or see some examples in the comments below. For example:

regexp_substr('abc_def_ghi','[^_]+',1,3);

==> ghi.

would you take $1000 a month to do what you were already doing?

Netscape is reportedly offering money to lure successful digg posters over to post at the new netscape.com a digg rip-off. There are some interesting comments on the article. Would you take the money? I would...

theory on movies

I have a theory that a movie based on a play has a higher chance of being good. Plays have to work without action scenes. The dialogue must be sound and snappy otherwise the play will bomb. This is obviously not the case for movies, eg [insert example bad movie here]. Here is a list of movies that helped me form my theory:
  • Hurly Burly
  • Rosencrantz & Guildenstern Are Dead
  • SubUrbia
I have another theory that states that any movie with Parker Posey in it has to be good, at least when Parker is in a scene. (See third example above). This leads me to believe that Superman might be worth seeing. It hits one out of two of my good movie indicators.

I saw Pirates of the Caribbean: Dead Man's Chest. It was pretty good. The action scenes have taken a leap towards the completely ridiculous but it didn't bother me like it did in Charlies Angels II. And the voodoo lady... wow! How is it possible that a woman with back teeth and red eyes could be so incredibly beautiful? I don't know but the voodoo lady was worth the price of admission for me. My one gripe: Davy Jones had too many tentacles. Surely he would have had just eight.

Tuesday, July 18, 2006

Animated music thing

Tuesday, July 11, 2006

Thom Yorke - new song live

If you're like me and the sound of Thom holding any tuneful vowel sound longer than 4 seconds has a reasonable chance of inducing involuntary goosebumps no matter how unlistenable the song then click here to see Thom live and accoustic on the Henry Rollins internet TV show. (Did that sentence need commas?) And it's almost listenable. Apparently he has a new solo album coming out soon. Via Stereogum

Sunday, July 09, 2006

Jack Black's Talent

I'm a huge fan of Jack Black and Tenacious D. School of Rock is one my favourite movies. Let alone his comedic talent, his musical talent truly would slay demons. Hope you enjoy this as much as I did. I'm sure I hummed this tune for days after just one listen. It's his song about King Kong from his spot on SNL (a show that, thanks to the miracle of bit-torrent, I no longer have to watch via three year old repeats on Arena TV). Brought to you by some guy on youtube.

Sunday, July 02, 2006

Disco Tex and his Sex-o-lettes

There's a Pet Shop Boys song on their 1996 album, Bilingual, that goes "It's the greatest show with the best effects since Disco Tex and the Sex-o-lettes". Well I never really knew that Disco Tex and the Sex-o-lettes were real, until Saturday when I found this: I don't have a record player so I can't tell you what they sound like. But judging by the cover and the Pet Shop Boys lyric it's going to be pretty special. Probably better live though. I don't generally dig through op-shop records. This one was on the top. So it's quite lucky that you're getting to see it. :) Check out the Amazon Reviews.