in reply to Golf - Ordinal Suffixes

Here's my first crack, coming in at 53.
sub num2ord { my $n = shift; # 1 2 3 4 5 6 +7 # 12345678901234678901234567890123456789012345678901234567890123456789 +0 $n.($n=~/1.$/?'th':qw(th st nd rd)[$n=~/([123]?)$/]) }
Update: and here's my 40-char entry:
sub num2ord { my $n = shift; # 1 2 3 4 5 6 +7 # 12345678901234678901234567890123456789012345678901234567890123456789 +0 $n.(qw(th st nd rd)[$n=~/(1?\d)$/]||th) }
It's not strict-safe, and will throw a warning if warnings are turned on. And I can trim one more character off, but I'll leave that up to you to find!

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Golf - Ordinal Suffixes
by sauoq (Abbot) on Oct 27, 2005 at 03:54 UTC

    Very nice. Here's my 40 chars... It runs under strict, anyway...

    sub num2ord { local $_ = shift; # 1 2 3 4 5 6 +7 # 12345678901234678901234567890123456789012345678901234567890123456789 +0 $_.qw(th st nd rd)[/(?<!1)([123])$/*$1] }
    And, combining ours can get us down to 35...

    -sauoq
    "My two cents aren't worth a dime.";
    
      Your way to shave a character (/msg'd) and my way are different, thus we can get it down to 34:
      sub num2ord { local $_ = shift; # 1 2 3 4 # 123456789012346789012345678901234567890 $_.(qw(0 st nd rd)[/1?\d$/g]||th) }

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

        We're assuming good input anyway, so we can shave another off...

        sub num2ord { local $_ = shift; # 1 2 3 4 # 123456789012346789012345678901234567890 $_.(qw(0 st nd rd)[/1?.$/g]||th) }
        Which drops us to 33.

        -sauoq
        "My two cents aren't worth a dime.";