Simplify your life.
"there's no requirement to use them all up before repeating. Just that I shouldn't re-use the same one twice or more in a row"
So you really don't need to keep track of anything more than the last one used.
I would use a simple text file with a single line containing the last value used. Read the last value from the file when you start, randomly select a values from your list until you get a 'new' value that doesn't match the previous (hopefully on the first try), then write the new value back to the file, replacing the previous one.
To do anything more would simply be a case of swating a mosquito with a with a lazer guided cruise missle (IMHO). Of course, this solution doesn't allow for much in the way of future enhancement.
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);
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.