Reputation: 21
Found this inside a loop. I've read up about splice but it just confused me more. I'm not familiar with Perl, but am trying to translate an algorithm to another language.
my $sill = splice(@list,int(rand(@list)),1);
last unless ($sill);
To be more specific: What will be inside $sill if it doesn't exit the loop from the last?
Thanks for any help!
Upvotes: 2
Views: 84
Reputation: 185106
That means :
list
(0 -> numbers of element of the list) andsill
variable with the removed element (pop()
like) andsill
variable is falseSee http://perldoc.perl.org/functions/splice.html
Upvotes: 2
Reputation: 57600
This randomly removes one element from the array @list
. That value is assigned to $sill
. If that was a false value, the enclosing loop (not shown) is broken out of.
splice
takes an array, an offset, and a length, plus a replacement list. If the replacement is omitted, the elements are deleted.
The length is constant (1 element), but the offset is calculated as a random integer smaller between 0 and the length of @list
.
Upvotes: 4