in reply to removing the goto
I had the benefit of a direct discussion with the OP about this, which brought much clarity as to his intentions. He has an array of ~4000 elements and wants to select five non-duplicates out of there. So basically, his code can be reduced to this:
Update: The big array is *not* already internally unique after all, so I had to add the line to unique it.
Update 2: My solution is no longer relevant, after the OP updated with more information. However, I'm leaving my code as-is because it might help a future monk looking for a similar solution.
---
It's all fine and dandy until someone has to look at the code.
I noticed he was randomizing the array, then selecting random indices from there, which was not only reduntdant but created the issue of potentially selecting the same element.use List::Util 'shuffle'; my %unique = map { $_ => 1 } @weighteddiv; my @shuffled = shuffle(keys %unique); @selected = @shuffled[0..4];
Update: The big array is *not* already internally unique after all, so I had to add the line to unique it.
Update 2: My solution is no longer relevant, after the OP updated with more information. However, I'm leaving my code as-is because it might help a future monk looking for a similar solution.
---
It's all fine and dandy until someone has to look at the code.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: removing the goto
by jdporter (Chancellor) on Jun 01, 2007 at 18:27 UTC | |
by imp (Priest) on Jun 01, 2007 at 18:43 UTC |
In Section
Seekers of Perl Wisdom