in reply to Looping through an Array of Hashes

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.

Replies are listed 'Best First'.
Re: Re: Looping through an Array of Hashes
by screamingeagle (Curate) on Feb 23, 2002 at 07:07 UTC
    very minor thing... u dont need to declare my %m = (); since its not being used anywhere...
    ++for your post, though :-)