Thinkgeek Gadget for GoogleFor a long time Google has been offering so-called gadgets that you could put on your personalised homepage. Thanks to the gadgets you get the weather report, google video, digg news or a calender to your homepage. Not so long ago Google published a new feature: now it is even possible to put those gadgets on your real homepage.

Of course there are lots of official and even more unoffical gadgets in the directory, but you know what is missing? A gadget for Thinkgeek, the ultimate gadget store (to be exact: there are rss-feeds for thinkgeek you can add to your google homepage, but not real gadgets). So why not create one?

The Google API pages offer extended information on how to build a gadget. The main part of every gadget is a small xml definition of the gadgets properties, requirements and perhaps code. The recommended way of filling the gadget is by putting html and javascript directly into this xml file. Unfortunately I was unable to build my gadget that way because I always got a javascript error in the google code. As always the Google code is badly scrambled so I didn’t spend much time looking for the error. (Anyways, if anyone finds a solution to “ig_a[ig_g] is not a function” - send me a mail)

Another way to build a gadget is by simply specifying a url that contains the gadget content. After javascript didn’t work I went to build a simple perl script that parses the thinkgeek rss and generates an html output. This worked fine and the gadget now displays a random thinkgeekd product on every load.

If you want to give it a try, click here: Add to Google

This is not really news, but I just rediscovered this: The ultimate regular expression for email address validation. As far as I know this more than 6k masterpiece was originally published in “Mastering Regular Expressions“: Code at Text*Snippets

So what is the fastest way to count how often a substring occurs inside a string, e.g. how many dashes can be found in “1-4-7-8-37-5-7-8-3-42″? I identified five ways to make perl say “9″ to this challenge:

  1. my $size = (scalar(@{[$string =~ /-/g]}) + 1);
  2. my $size = scalar(split /-/, $string);
  3. my $size = (($string =~ s/-//g) + 1);
  4. my $size = (($string =~ tr/-//) + 1);
  5. my $size = 1; $size++ while $string =~ /-/g;

I would have assumed that number 1 would be the fastest way as it requires no modifcation of the string. A quick run with “cmpthese” from the Benchmark module revealed that my assumption was wrong:

Rate match (1) split (2) while (5) repl (3) tr (4)
match (1) 51106/s -90% -93% -94% -95%
split (2) 495540/s 870% -36% -45% -47%
while (5) 771605/s 1410% 56% -15% -18%
repl (3) 908265/s 1677% 83% 18% -4%
tr (4) 941620/s 1742% 90% 22% 4%

The table shows a clear advantage when counting the occurences via a while-loop (solution 5), count the replaced characters (solution 3) or truncating all occurences (solution 4). While “tr” was the fastest way during this test run, it wasn’t the best way on other benchmark runs or others systems. If you want to try it yourself you can do so with the attached test-script.

Attached Files: 0K