koldfront

Beautiful Code: Finding Things #programming

🕣︎ - 2007-10-17

A month ago I ordered some books. They finally arrived. One of them is "Beautiful Code", which contains 33 chapters by diffent authors on, well yes, beautiful code.

Chapter 4 is called "Finding Things" and is written by Tim Bray. He uses Ruby, regular expressions and associative arrays to analyse logfiles from his webserver.

His reason for choosing Ruby is: "because I believe it to be, while far from perfect, the most readable of languages."

Futher down the line he ponders the speed of one of his examples and compares it to a (not listed) equivalent Perl-implementation, which takes half the time to run.

Be that as it may, I think I will use a couple of posts to compare his Ruby examples with Perl equivalents (probably riddled with errors :-))

Example 4-1:

ARGF.each_line do |line|
  if line =~ %r{GET /ongoing/When/\d\d\dx/\d\d\d\d/\d\d/\d\d/[^ .]+ }
    puts line
  end
end

I guess it would go something like this in Perl:

Example 4-1':

while (my $line=<>) {
  if ($line=~|GET /ongoing/When/\d\d\dx/\d\d\d\d/\d\d/\d\d/[^ .]+ |) {
    print $line;
  }
}

An improvement in both programs could be to use the \d{4} idiom. The Perl-version could use postfix if, but the condition is a little overwhelming for that.

Which is prettier?

Add comment

To avoid spam many websites make you fill out a CAPTCHA, or log in via an account at a corporation such as Twitter, Facebook, Google or even Microsoft GitHub.

I have chosen to use a more old school method of spam prevention.

To post a comment here, you need to:

¹ Such as Thunderbird, Pan, slrn or Gnus (part of Emacs).