This could be rewritten as:
#!/usr/bin/perl -w
# Make an array of hashes.
# This part works.
use strict; # <- Note the use strict
my @months = ();
my %m = ();
push @months, {
full => "january",
abbrev => "jan",
number => 1,
};
push @months, {
full => "february",
abbrev => "feb",
number => 2,
};
push @months, {
full => "march",
abbrev => "mar",
number => 3,
};
# etc
# Now try to print it out.
foreach my $month (@months) {
print "$month->{full}\n";
print "$month->{abbrev}\n";
print "$month->{number}\n\n";
}
This gets rid of the temp %m's you were creating as well
and avoid the problem of the last reference wining, which
is what you would have seen if you had changed the while
to a foreach without changing the way you did your push.
You should also check out
CPAN
for date modules that might
save you some time.
If you need to look at the contents of datastructure and not
perform any action, that is verify content, you should look
into using
Data::Dumper, it would remove the need for the
foreach and prints a nice tidy view of your data.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.