Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

convert month number to month name

by Shaveta_Chawla (Acolyte)
on Aug 30, 2012 at 06:47 UTC ( #990655=perlquestion: print w/ replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

I am using
my($day, $month, $year)=(localtime)[3,4,5]; $year = $year + 1900;

to get system local time. but i want to get numeric month to month name(8 should change to aug) without creating any array/hash ,and store the value in $month variable.

it would be helpful if no extra module is used.

. Thanx in advance

Comment on convert month number to month name
Download Code
Re: convert month number to month name
by nemesdani (Friar) on Aug 30, 2012 at 06:57 UTC
    Why are you against creating a new variable? Does your code run on an embedded system, and you're running out of memory?*
    You could if-elsif your way through the calendar as a basic solution.


    * I know, I know...

    I'm too lazy to be proud of being impatient.
Re: convert month number to month name
by 2teez (Priest) on Aug 30, 2012 at 07:08 UTC
    Hi,

    Hash is up to the rescue

    my %month_name; @month_name{ 0 .. 11 } = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct N +ov Dec); my ( $day, $month, $year ) = (localtime)[ 3, 4, 5 ]; $year = $year + 1900; print join " ", $day, $month_name{$month}, $year;
    output
    30 Aug 2012

      I'm not seeing the point of using a hash here... Since the hash keys are a contiguous, zero-based range of integers, why not just use an array instead of making a hash behave like one?
      my @month_name = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec); my ( $day, $month, $year ) = (localtime)[ 3, 4, 5 ]; $year = $year + 1900; print join " ", $day, $month_name[$month], $year;
Re: convert month number to month name
by cztmonk (Monk) on Aug 30, 2012 at 07:08 UTC

    Try this:

    use Modern::Perl; my($day, $month, $year)=(localtime)[3,4,5]; $year = $year + 1900; $month++; given ($month) { when ("1") {$month = "January" } when ("2") {$month = "February" } when ("3") {$month = "March" } when ("4") {$month = "April" } when ("5") {$month = "May" } when ("6") {$month = "June" } when ("7") {$month = "July" } when ("8") {$month = "August" } when ("9") {$month = "September" } when ("10") {$month = "October" } when ("11") {$month = "November" } when ("12") {$month = "December" } } print $month; __END__ August
Re: convert month number to month name
by Anonymous Monk on Aug 30, 2012 at 07:10 UTC

    it would be helpful if no extra module is used

    Why?

Re: convert month number to month name
by philiprbrenan (Monk) on Aug 30, 2012 at 07:27 UTC

    Have you considered using strftime() from the POSIX module? The parameters to strftime() can be found here: http://linux.die.net/man/3/strftime

Re: convert month number to month name
by davido (Archbishop) on Aug 30, 2012 at 07:33 UTC

    I believe you're asking to convert the month number (zero based) into a month name, and store that name in the variable where the number had previously resided.

    use strict; use warnings; my ( $day, $month, $year ) = ( localtime )[3,4,5]; $month = ( qw/ January February March April May June July August September October November December / )[$month]; print $month, "\n";

    The constraint on not using any module or intermediary variables is a little odd. It's a small enough problem (assuming this is the entirety of the problem) that a module wouldn't make much sense. But the requirement to store the result in the original variable without any intermediate variable, while not hard to accomplish, reads like an interview or test question.

    If you don't need to store the day and year, you could even get away with a one-liner, and no variables at all:

    perl -e 'print +(qw/January February March April May June July August +September October November December/)[(localtime)[4]],$/;'

    Not very useful though. ;)


    Dave

      The constraint smells like homework
Re: convert month number to month name
by Shaveta_Chawla (Acolyte) on Aug 30, 2012 at 07:36 UTC
    Isn't there any perl built in function which can do the work, like there is strftime function which can do the work.

      POSIX is a core module so strftime() is available without the need to install any modules from CPAN.

      $ perl -E ' > use POSIX qw{ strftime }; > say strftime q{%b}, localtime( time() );' Aug $

      Cheers,

      JohnGG

Re: convert month number to month name
by Shaveta_Chawla (Acolyte) on Aug 30, 2012 at 09:53 UTC
    Another method which can be used is
    $YEAR=`date "+%Y"`; #for year $month=`date "+%b"`; #convert numeric month to month name $DAY=`date "+%d"`; #for date

      ... which fails in a Windows-environment. There the command date expects a new date as user-input.

Re: convert month number to month name
by Marshall (Prior) on Aug 30, 2012 at 10:09 UTC
    Define a hash table with the translation that you desire. This will be compiled - it only happens once. Then just access that hash table to print the text that you desire.
    #!/usr/bin/perl -w use strict; my %xlateNum2Text = qw (0 January 1 Febuary 2 March 3 April 4 May 5 June 6 July 7 August 8 September 9 October 10 November 11 December ); my($day, $month, $year)=(localtime)[3,4,5]; $year = $year + 1900; print "day=$day, month=$month, year=$year\n"; print "day=$day, month=$xlateNum2Text{$month}, year=$year\n"; __END__ Prints: day=30, month=7, year=2012 day=30, month=August, year=2012
    Update:
    Yes, it is possible to use an array instead of a hash table. However, I would recommend making the intent and translation very clear by using a hash table. In the code above, it is clear what number corresponds to what text, ie, that January is zero, not one.
Re: convert month number to month name
by Skeeve (Vicar) on Aug 30, 2012 at 12:01 UTC

    > 8 should change to aug

    Just on a side note: 8 should not change to "Aug". It should change to "Sep". 7 should change to "Aug". The month is zero-based,


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: convert month number to month name
by roboticus (Canon) on Aug 30, 2012 at 12:44 UTC

    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.

      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'
         printf q['%s'], unpack('x' x ($month * 3) . 'A3', 'JanFebMarAprMayJunJulSepAugOctNovDec');

        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.

Re: convert month number to month name
by Anonymous Monk on Aug 31, 2012 at 00:46 UTC

    Hi,

    Simplest I can think of -

    my($day, $month, $year)=(localtime)[3,4,5]; $year = $year + 1900; $month = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct N +ov Dec)[$month]; print $month;

    J.C.

Re: convert month number to month name
by DrHyde (Prior) on Sep 03, 2012 at 10:49 UTC
    What have you tried so far?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990655]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-08-01 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (50 votes), past polls