in reply to Re: convert month number to month name
in thread convert month number to month name

Shaveta_Chawla wants 8 to convert to 'Aug', so the code should really be:

>perl -wMstrict -le "my $months = 'JanFebMarAprMayJunJulSepAugOctNovDec'; my $month = 8; printf q{'%s'}, substr($months, $month*3, 3); " 'Aug'

Replies are listed 'Best First'.
Re^3: convert month number to month name
by roboticus (Chancellor) on Aug 30, 2012 at 14:37 UTC

    AnomalousMonk:

    Hmmm ... why not map *all* of 'em to Aug?

    my $months = 'AugAugAugAugAugAugAugAugAugAugAugAug'; my $month = 8; printf '%s', substr($months, $month*3, 3);

    Then we could simplify it a bit:

    my $month = 8; printf '%s', substr('Aug'x12, $month*3, 3);

    ;^)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      It's true that the specification for mapping of indices other than 8 is undefined, so your further refinements are useful.   ++ to you, sir, and well played!

Re^3: convert month number to month name
by Anonymous Monk on Aug 30, 2012 at 14:34 UTC
     printf q['%s'], unpack('x' x ($month * 3) . 'A3', 'JanFebMarAprMayJunJulSepAugOctNovDec');