Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

English Date-Suffixes

by Cody Pendant (Prior)
on Nov 04, 2002 at 03:26 UTC ( #210112=perlmeditation: print w/replies, xml ) Need Help??

I saw this in someone's post:
@monthPost = ( "-", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st" ); $textInput = "$mday$monthPost[$mday]" # etc.
and it reminded me that I once set off a little golf on that topic. The winner was this (sub) from Jonas Nilsson:
$_[0]=~/1\d$/?0:qw(0 st nd rd)[$_[0]%10]or'th';
($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Replies are listed 'Best First'.
Re: English Date-Suffixes
by cLive ;-) (Prior) on Nov 04, 2002 at 07:54 UTC
    You can get it down another 6 chars to 41 by a little rewriting, and the $ in the regex and final semi-colon aren't needed:
    shift;/1\d/?0:qw(0 st nd rd)[$_%10]or'th'


    cLive ;-)

      If you can live with the risk of the sub stomping on the callers copy of $_ by not localising it, then you could trim a couple more keystrokes by using pop instead of shift, and you also can trim the regex by another 1

      pop;/1./?0:qw(0 st nd rd)[$_%10]or'th'

      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
      Is this what you want? (Neither pop nor shift load $_ by defalult -- they just default to using @_ in a sub or @ARGV in the main prog.)
      $_=pop;qw(0 st nd rd)[!/1./&&$_%10]or'th'

Re: English Date-Suffixes
by BrowserUk (Pope) on Nov 04, 2002 at 05:19 UTC

    There are still a couple of loose ones in there. -2 or -3 if you count the superfluous ;

    ($£=pop)=~/1./?0:qw(0 st nd rd)[$£%10]or'th'

    My best alternate ain't even close, substr is too long.

    sub nthit{ substr'thstndrd',($£=pop)=~/1./||($£%=10)>3?0:2*$£,2 }

    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
      What's with the accent marks and sterling pounds? I read that as a variable name, but why use two chars for an arbitrary name? Must be something special, or something else that got displayed wrong.

        In essence, any single char scalar name would do.

        I started using as a variable name in golf-type solutions as its the only punctuation global that isn't used for something somewhere by Perl itself, therefore it doesn't cause warnings with strict, and I don't have to localise it.

        Quite why one instance of the £ character got converted during the process of posting the code, and none of the others did I am not quite sure. I even tried to correct it by cut&pasting one of the others that shows correctly (in my browser at least) over the top of the one that comes out wrong, but the results where always the same. I have no idea how to correct it.

        Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: English Date-Suffixes
by petral (Curate) on Nov 06, 2002 at 16:34 UTC
    pop=~/1?./;qw(0 st nd rd)[$&]or'th'

Re: English Date-Suffixes
by petral (Curate) on Nov 07, 2002 at 15:43 UTC
    And qw(0 st nd rd)[pop=~/(1?.)$/]or"th"
    will do any integer.

    Note that for pre-5.6 perl you have to do (qw(...))[index]
    because qw was translated into  split' ','...' during compilation.


      small improvement:

        Wow, didn't know about dropping the qw!
        Why does the ||th  lead to only returning the last value from the /1?./g -indexed list?

        Update:  I see, it becomes a comma list which returns the last value ("the result of the last expression").
        see, A list returns its last, an array returns its weight


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://210112]
Approved by tadman
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-04-20 01:35 GMT
Find Nodes?
    Voting Booth?