use strict; use warnings; my @ids = qw(a b c d e f g h); my $chosen_id = ''; dbmopen( my %recent_ids, 'recent', 0777 ) || die "$!"; while (1) { $chosen_id = @ids[ int( rand(@ids) ) ]; print "chosen: $chosen_id\n"; if ( !defined( $recent_ids{$chosen_id} ) ) { last } else { print "problem: $chosen_id is on the recent list, choose again\n" } } $recent_ids{$chosen_id} = time(); my $count = 0; foreach ( sort { $recent_ids{$b} <=> $recent_ids{$a} } ( keys(%recent_ids) ) ) { $count++; if ( $count > 3 ) { print "$_ is the oldest id, remove it\n"; delete( $recent_ids{$_} ); } } print "hash is now:\n"; while ( my ( $key, $value ) = each(%recent_ids) ) { print "$key - $value\n"; } dbmclose(%recent_ids);