use Date::Calc qw(Mktime Decode_Month); use constant { TIME => 0, POST => 1 }; use constant num_posts => 2; my @recent; dbmopen(my %NEWS, "news", 0644) #opening the db or die("Error opening news database: $!"); while (my ($localtime, $post) = each %NEWS) { my ($weekday, $mon, $day, $hour_min_sec, $year) = split /\s+/, $localtime; my $time = Mktime($year, Decode_Month($mon), $day, split /:/, $hour_min_sec); if (@recent < num_posts) { @recent = sort { $a->[TIME] <=> $b->[TIME] } @recent, [$time, $post]; } elsif ($time > $recent[0]->[TIME]) { shift @recent; @recent = sort { $a->[TIME] <=> $b->[TIME] } @recent, [$time, $post]; } }