use strict; use warnings; my @ids = qw(a b c d e f g h); my $chosen_id = ''; my $last_id = ''; if (open (my $fh, 'recent')) { $last_id = <$fh>; close ($fh); } while (1) { $chosen_id = @ids[ int( rand(@ids) ) ]; print "chosen: $chosen_id\n"; last if $chosen_id ne $last_id; print "problem: $chosen_id is on the recent list, choose again\n"; } # replace the file open (my $fh, '>', 'recent') or die "failed to open recent for write: $!\n"; print $fh $chosen_id; close ($fh);