koldfront

Beautiful Code: Finding Things #programming

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?

Title:

Name:

Email (won't be displayed online):

Text:

0.0163 s
webcustodian@koldfront.dk