I ran across an old function I had written to take a non-negative integer and append the correct ordinal suffix. (I.e. 'th', 'st', 'nd', or 'rd') Out of boredom, I started golfing. Here's an early attempt coming in at 70 strokes:
sub num2ord2 { my $n = shift; # 1 2 3 4 5 6 +7 # 12345678901234678901234567890123456789012345678901234567890123456789 +0 $n.(qw(th st nd rd),('th')x16,((qw(th st nd rd),('th')x6)x8))[$n%100 +] }
But... I can do it in 40†... Can you beat that?
† It does generate a warning, however.
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Golf - Ordinal Suffixes
by japhy (Canon) on Oct 27, 2005 at 02:51 UTC | |
by sauoq (Abbot) on Oct 27, 2005 at 03:54 UTC | |
by japhy (Canon) on Oct 27, 2005 at 04:21 UTC | |
by sauoq (Abbot) on Oct 27, 2005 at 05:02 UTC | |
by hv (Prior) on Oct 27, 2005 at 09:18 UTC | |
| |
|
Re: Golf - Ordinal Suffixes
by zshzn (Hermit) on Oct 27, 2005 at 23:05 UTC | |
by sauoq (Abbot) on Oct 27, 2005 at 23:08 UTC |