http://qs321.pair.com?node_id=618798

scoobyrico has asked for the wisdom of the Perl Monks concerning the following question:

I have a piece of code that has worked a long time, but I know there has got to be a better way to do this. I dislike gotos but that was the best I could come up with at the time. Any help would be much appreciated! Thanks in advance! My goal is to get the most random selection of items from the @weighteddiv array to the @selected array without duplicating common numbers, i.e. the @selected array can not have a duplicate number. The values are int 4 digits, like 2345. The @weighteddiv will have 1...N numbers at various frequencies, like 2200 of 2161, 1854 of 3222, 50 of 2344 and so forth. Hopefully this clears up more of what this code is suppose to do.
use List::Util shuffle; @weighteddiv = shuffle @weighteddiv; @selected = (); foreach $i (0..$maxclients-1) { xyz: if ($#weighteddiv > = 0) { $idx=int(rand($#weighteddiv)); $selected[$i] = $weighteddiv[$idx]; if ($i>0) { foreach $j (0..$i-1) { if ($selected[$i] == $selected[$j]) { goto xyz } } } } }