Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: convert month number to month name

by Marshall (Prior)
on Aug 30, 2012 at 10:09 UTC ( #990710=note: print w/ replies, xml ) Need Help??


in reply to convert month number to month name

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.


Comment on Re: convert month number to month name
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2015-07-31 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls