Main Content

A few of my favourite things

Archive - Originally posted on "The Horse's Mouth" - 2008-10-26 00:45:35 - Graham Ellis

"Raindrops on roses and whiskers on kittens; Bright copper kettles and warm woollen mittens ..."

Sorry - the raindrops get me all wet, and I always get caught up on the thorns of roses. Mittens restrict the hands and make me even more butter-fingered than I am normally ... isn't it great that we're all different - we all have different favourite things!

I was in Cambridge last week, running a Perl course - and someone asked me which my favourite programming language was. Now there's a tough question, but it did lead on to a demonstration - in Perl because that's what the course was - that showed the favourite things that bring people to our web site from Google. Here are the results - showing the top words for 23rd October!

$ perl search_fodder
2385 - Perl
2055 - Java
1850 - In
1844 - Php
1754 - Python
1331 - Mysql
1003 - To
880 - Example
870 - Join
739 - Tcl
Total 14416 searches and 118619 others
0.71 0.13 0 0
$



Here's the code ... showing some really very bad things (like a complete lack of comments) and some really "wicked" things where Perl is such a passed master!

open (FH,"ac_20081023") or die;
while (<FH>) {
  if (/[&?]q=(.*?)[&"]/) {
    $ss = ;
    $seaches++;
    @words = split(/\++/,$ss);
    foreach $w (@words) {
      $w =~ s/%(..)/pack("C",hex())/eg;
      $sw{ucfirst(lc($w))}++;
      }
  } else {
    $others++;
  }
}
@words = sort {$sw{$b} <=> $sw{$a}} keys %sw;
foreach $word(@words[0..10]) {
  print "$sw{$word} - $word\n";
  }
print "Total $seaches searches and $others others\n";
@taken = times();
print "@taken\n";


You'll note ...
• Use of $_
• Regular expressions to decode URLs
• Sparse matching in a regular expression
• A hash of counters
• List Slice syntax to report just "top 10" results
• Anonymous sort subroutine
• A timer to tell me how much resource was being used.

If you can identify each of those, great ... if you need help in identifying them, perhaps I should run a Perl Programming Course for you!