Bill Lovett [mailto:[EMAIL PROTECTED]] asked:
>
> Can anyone suggest a better/cooler way to populate this hash? I'm
> doing ultra simple date checking, so this is all I really need, but
> there must be another way to do it...
>
> my %monthNames;
> $monthNames{"Jan"} = 0;
> $monthNames{"Feb"} = 1;
> $monthNames{"Mar"} = 2;
> $monthNames{"Apr"} = 3;
> $monthNames{"May"} = 4;
> $monthNames{"Jun"} = 5;
> $monthNames{"Jul"} = 6;
> $monthNames{"Aug"} = 7;
> $monthNames{"Sep"} = 8;
> $monthNames{"Oct"} = 9;
> $monthNames{"Nov"} = 10;
> $monthNames{"Dec"} = 11;
>
First of all, date processing is notoriously tedious and error-prone, so use
a module and avoid some pain:
http://search.cpan.org/search?mode=module&query=Date%3A%3A
But to answer your original question... you can initialize the data
structure with a hash slice:
@monthNames{qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )} =
0..11;
If you're going from numbers to names, then you probably want an array
instead of a hash. That way, you can use the month number directly as an
offset into a list:
@monthNamesArray = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
$monthName = $monthNamesArray[0]; # January
(Of course, if you're parsing human-entered dates, then January will
correspond to 1, not 0, and you'll have to adjust your logic accordingly.)
You can use a combination of these techniques for two-way validation.
Did I mention you should use a module? :)
--Bill
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]