Thoughts / Misc: May 2006 Archives

13:58:29 srbaker : i've had half a dozen folks look over this, including michael granger
13:58:42 srbaker : i've stripped it down to the bare basics: ie: so simple it can't be wrong.
13:59:04 zenspider : "so simple it can't be wrong" can I quote you on that???
13:59:27 srbaker : oh god no.

why not perl?

| | Comments (4)

I was recently asked this by a recruiter for ticketmaster (a big perl shop):

Quick question if you don't mind. Why not working in perl? You have perl in your background so it's always interesting for me to learn why people move from one language or environment to another. I hope you don't mind "sharing." ;)

My reply:

Perl sucks. Really. It is a horrifically bad language at expressing thoughts cleanly and clearly and from having a maintainable, well-engineered solution. I'm good at it (or was--I dunno about now, but hey, it's Just a Language (tm)), don't get me wrong. I've cut a lot of code in perl professionally and on the side. It just doesn't scale in the long term.

No... I don't mind sharing, as long as you don't mind me blogging it as well. :)

Interestingly, I ported the last of my perl packages to ruby last year. Despite not having worked on it for well over 4 years it was still readable!! It really does depend on the person, but if you're working for a perl shop... I guarantee that there's always that one person (or many) who writes that hideous noxious code.

No thank you.

Original Request:

I'd like to sort an array of strings numerically...

Modifications:

... but that doesn't handle letters (non-number strings)
... mine was shorter, but it uses an intermediate variable
... nice, but how do you get the digits before the letters?

My final version:

%w(z A 1 10 2 b).sort_by { |s| [ s[0], s.to_i, s ] }

(ugh)

Update:

kodis did a good job pointing out that I shouldn't blog at 2am. The above code is buggy. Namely, the first term in the sort_by is flat out wrong and was a quick add right at the end that I didn't do a good job of testing. So, the elegant version is:

%w(z A 1 10 2 b).sort_by { |s| [ s.to_i, s ] }

but the one that will sort numbers first (no idea why still, but hey) is:

%w(z A 1 10 2 b).sort_by { |s| [ s =~ /^\d/ ? 1 : 2, s.to_i, s ] }

still, even that ugly sort_by is vastly better than my first attempt:

%w(z a 1 10 2 b).map {|o| Integer(o) rescue o }.partition { |o|
  Fixnum === o }.map { |a| a.sort }.flatten.map { |o| o.to_s }

blech!

About this Archive

This page is a archive of entries in the Thoughts / Misc category from May 2006.

Thoughts / Misc: April 2006 is the previous archive.

Thoughts / Misc: June 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.1