Re: convert month number to month name
by davido (Cardinal) 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. ;)
| [reply] [d/l] [select] |
|
The constraint smells like homework
| [reply] |
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
| [reply] |
Re: convert month number to month name
by nemesdani (Friar) on Aug 30, 2012 at 06:57 UTC
|
| [reply] [d/l] [select] |
Re: convert month number to month name
by 2teez (Vicar) 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
| [reply] [d/l] [select] |
|
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;
| [reply] [d/l] |
Re: convert month number to month name
by roboticus (Chancellor) on Aug 30, 2012 at 12:44 UTC
|
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. | [reply] [d/l] |
|
>perl -wMstrict -le
"my $months = 'JanFebMarAprMayJunJulSepAugOctNovDec';
my $month = 8;
printf q{'%s'}, substr($months, $month*3, 3);
"
'Aug'
| [reply] [d/l] |
|
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. | [reply] [d/l] [select] |
|
|
printf q['%s'], unpack('x' x ($month * 3) . 'A3', 'JanFebMarAprMayJunJulSepAugOctNovDec');
| [reply] [d/l] |
Re: convert month number to month name
by cztmonk (Monk) on Aug 30, 2012 at 07:08 UTC
|
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
| [reply] [d/l] |
Re: convert month number to month name
by Skeeve (Parson) 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
| [reply] [d/l] [select] |
Re: convert month number to month name
by Anonymous Monk on Aug 30, 2012 at 07:10 UTC
|
| [reply] |
Re: convert month number to month name
by Marshall (Canon) 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. | [reply] [d/l] |
Re: convert month number to month name
by Shaveta_Chawla (Sexton) 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
| [reply] [d/l] |
|
| [reply] |
Re: convert month number to month name
by Anonymous Monk on Aug 31, 2012 at 00:46 UTC
|
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. | [reply] [d/l] |
Re: convert month number to month name
by DrHyde (Prior) on Sep 03, 2012 at 10:49 UTC
|
What have you tried so far? | [reply] |
Re: convert month number to month name
by Shaveta_Chawla (Sexton) 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. | [reply] |
|
$ perl -E '
> use POSIX qw{ strftime };
> say strftime q{%b}, localtime( time() );'
Aug
$
| [reply] [d/l] [select] |