I'd refactor the loop to eliminate the goto, the label, most of the nesting, and to make the intent a little clearer:
foreach my $i (0..$maxclients-1) {
$selected[$i] = $weighteddiv[int rand @weighteddiv];
next if $i == 0;
# Check for duplicates
redo if grep {$selected[$i] == $selected[$_]} 0 .. $i-1;
}
Update: s/next if/last if/ - bug pointed out by TimToady
Update: removed while loop - issue pointed out by TimToady
fixed bug due to next in foreach modified statement
fixed domain for rand - issue pointed out by TimToady
DWIM is Perl's answer to Gödel
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|