Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: convert month number to month name

by Marshall (Abbot)
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
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990710]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-04-20 23:14 GMT
Find Nodes?
    Voting Booth?