For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > get full month name









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author get full month name
bbrecht56@gmail.com

2007-12-08, 7:59 am

I'm trying to write a subroutine that returns the full month name, for
example January, or February when I call the subroutine and pass a
scalar, for example $m that could have a value in one of the following
format

1. three letter format, for example jan or feb, or
2. one or digit format for example "01" or "1" for january

Example:

sub returnFullmonth{

}

from the main program:

$month ="Feb";
#or $month = "02" or "2";

$fullMonth = &returnFullmonth($month);

print "Month: $fullMonth";
expected out put: Month: February

can some help me and tell me how to write the subroutine?

Many thanks in advance.

bbrecht

Eric Krause

2007-12-08, 7:59 am

I'm also a perl newbie, but couldn't you use a hash? Something like:
%fullMonth = ( jan => January,
feb => February,
mar => March,
etc...);
print "Month: $fullMonth{$month}";

-eric


bbrecht56@gmail.com wrote:
> I'm trying to write a subroutine that returns the full month name, for
> example January, or February when I call the subroutine and pass a
> scalar, for example $m that could have a value in one of the following
> format
>
> 1. three letter format, for example jan or feb, or
> 2. one or digit format for example "01" or "1" for january
>
> Example:
>
> sub returnFullmonth{
>
> }
>
> from the main program:
>
> $month ="Feb";
> #or $month = "02" or "2";
>
> $fullMonth = &returnFullmonth($month);
>
> print "Month: $fullMonth";
> expected out put: Month: February
>
> can some help me and tell me how to write the subroutine?
>
> Many thanks in advance.
>
> bbrecht
>
>

Dr.Ruud

2007-12-08, 7:00 pm

bbrecht56@gmail.com schreef:

> I'm trying to write a subroutine that returns the full month name, for
> example January, or February when I call the subroutine and pass a
> scalar, for example $m that could have a value in one of the following
> format
>
> 1. three letter format, for example jan or feb, or
> 2. one or digit format for example "01" or "1" for january



#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @m = qw(
NUL
January February March
April May June
July August September
October November December
);

my %m2m;
for (my $i = 1; $i <= $#m; $i++) {
$m2m{ lc substr $m[$i], 0, 3 } = $i;
};

print Dumper \@m;

print Dumper \%m2m;

print "$_: $m[ $m2m{ $_ } ]\n" for keys %m2m;

print "$_: $m[ $_ ]\n" for 1 .. 12, "01" .. "12";

__END__

--
Affijn, Ruud

"Gewoon is een tijger."

Chas. Owens

2007-12-08, 7:00 pm

On Dec 7, 2007 6:10 PM, <bbrecht56@gmail.com> wrote:
> I'm trying to write a subroutine that returns the full month name, for
> example January, or February when I call the subroutine and pass a
> scalar, for example $m that could have a value in one of the following
> format
>
> 1. three letter format, for example jan or feb, or
> 2. one or digit format for example "01" or "1" for january

snip

TIMTOWTDI. In addition to the ways I implemented it below you could
use a set of cascading hashes with ||s, a substitution, and myriad*
other ways.

#!/usr/bin/perl

use strict;
use warnings;
use Date::Manip;

sub using_date_manip {
my $month = shift or die "bad number of arguments";
my $d = ParseDate("2007-$month-01") or die "bad argument: [$month]";
return UnixDate($d, "%B");
}

{
my %month_map = (
'01' => 'January',
1 => 'January',
Jan => 'January',
jan => 'January',
'02' => 'February',
2 => 'February',
Feb => 'February',
feb => 'February',
etc => 'etc'
);
sub using_hash {
my $month = shift or die "bad number of arguments";
return $month_map{$month} || die "bad argument: [$month]";
}
}

for my $arg (qw(02 2 feb Feb)) {
print "using_date_manip($arg): ", using_date_manip($arg), "\n",
"using_hash($arg): ", using_hash($arg), "\n";
}

* yes, this is hyperbole, I doubt there are 10,000 unique methods
Jefferson Kirkland

2007-12-08, 10:05 pm

Well, to go along with the other example(s), here is a way of doing it using
the localtime() function:

use strict;
use warnings;

my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfW,
$dayOfYear, $daylightSavings) = localtime();

my $monthnum = $month + 1;

my %monthname = (
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
);

print("The present month is: $monthname{$monthnum}\n");

Regards,

Jeff

Jo for Groups and Lists

2007-12-09, 7:01 pm

Can you use Date::Manip ?


$fullMonth =3D &returnFullmonth($month);

sub returnFullmonth {=09
use Date::Manip;
return UnixDate("2007/$_/01",'%B');
}

This will handle feb, 02, or 2 but will not handle february itself - so
you may want to check first that you are indeed passing in 1-2 digits or
3 alphas.


Jo=20

Gunnar Hjalmarsson

2007-12-09, 7:01 pm

Jo for Groups and Lists wrote:
> Can you use Date::Manip ?
>
> $fullMonth = &returnFullmonth($month);
>
> sub returnFullmonth {
> use Date::Manip;
> return UnixDate("2007/$_/01",'%B');
> }
>
> This will handle feb, 02, or 2


No, it won't handle anything.

C:\home>type test.pl
use warnings;
$TZ = 'GMT';

$fullMonth = &returnFullmonth('02');

sub returnFullmonth {
use Date::Manip;
return UnixDate("2007/$_/01",'%B');
}

C:\home>test.pl
Name "main::fullMonth" used only once: possible typo at C:\home\test.pl
line 4.
Use of uninitialized value in concatenation (.) or string at
C:\home\test.pl line 8.

C:\home>

Arguments passed to a Perl sub are accessed via @_, not via $_. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Bertolt Brecht

2007-12-10, 10:03 pm

Thanks Chas and everyone else.
The solution worked perfect

Bbrecht


On Dec 8, 2007 7:35 AM, Chas. Owens <chas.owens@gmail.com> wrote:

> On Dec 7, 2007 6:10 PM, <bbrecht56@gmail.com> wrote:
> snip
>
> TIMTOWTDI. In addition to the ways I implemented it below you could
> use a set of cascading hashes with ||s, a substitution, and myriad*
> other ways.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use Date::Manip;
>
> sub using_date_manip {
> my $month = shift or die "bad number of arguments";
> my $d = ParseDate("2007-$month-01") or die "bad argument:
> [$month]";
> return UnixDate($d, "%B");
> }
>
> {
> my %month_map = (
> '01' => 'January',
> 1 => 'January',
> Jan => 'January',
> jan => 'January',
> '02' => 'February',
> 2 => 'February',
> Feb => 'February',
> feb => 'February',
> etc => 'etc'
> );
> sub using_hash {
> my $month = shift or die "bad number of arguments";
> return $month_map{$month} || die "bad argument: [$month]";
> }
> }
>
> for my $arg (qw(02 2 feb Feb)) {
> print "using_date_manip($arg): ", using_date_manip($arg), "\n",
> "using_hash($arg): ", using_hash($arg), "\n";
> }
>
> * yes, this is hyperbole, I doubt there are 10,000 unique methods
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com