in reply to convert month number to month name

Shaveta Chawla:

my $months = 'JanFebMarAprMayJunJulAugSepOctNovDec'; my $month = 8; print substr($months, $month*3, 3), "\n";

...roboticus

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

Replies are listed 'Best First'.
Re^2: convert month number to month name
by AnomalousMonk (Archbishop) on Aug 30, 2012 at 14:26 UTC

    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'

      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!

       printf q['%s'], unpack('x' x ($month * 3) . 'A3', 'JanFebMarAprMayJunJulSepAugOctNovDec');