Here is a much better data structure that also shows you how you can set a hash value to the return val of a sub and thus give feb a correct value. It also shows semi dynamic hash construction using map FWIW.
my @month_names = qw( undef, jan feb mar apr may jun jul aug sept oct
+nov dec );
my @month_days = ( undef, 31, &feb_days, 31, 30, 31, 30, 31, 31, 30, 3
+1, 30, 31 );
my $months = {
name => { map{ $_, $month_names[$_]} 1..12 },
number => { map{ $month_names[$_], $_ } 1..12 },
days => { map{ $_, $month_days[$_], $month_names[$_], $month_day
+s[$_] } 1..12 },
};
print "Month number 4 is ", $months->{name}->{4}, "\n";
print "Month mar is number ", $months->{number}->{mar}, "\n";
print "Days in month feb ", $months->{days}->{feb}, "\n";
print "Days in month 4 ", $months->{days}->{4}, "\n";
use Data::Dumper;
print Dumper $months;
sub feb_days { check_leap_year( get_year() ) ? 29 : 28 }
sub get_year { (localtime())[5] + 1900 }
sub check_leap_year {
my $year = shift;
my $leap_year = 0;
$leap_year = 1 if $year % 4 == 0;
$leap_year = 0 if $year % 100 == 0 and $year % 400 != 0;
return $leap_year;
}
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.