Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: removing the goto

by kwaping (Priest)
on Jun 01, 2007 at 17:23 UTC ( [id://618804]=note: print w/replies, xml ) Need Help??


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:
use List::Util 'shuffle'; my %unique = map { $_ => 1 } @weighteddiv; my @shuffled = shuffle(keys %unique); @selected = @shuffled[0..4];
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.

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 (Paladin) on Jun 01, 2007 at 18:27 UTC
    use List::Util 'shuffle'; use List::MoreUtils 'uniq'; my @shuffled = shuffle( uniq @weighteddiv ); @selected = @shuffled[0..4];
      It might be more appropriate to do the shuffle before the uniq if the OP wants the duplicate values to have a greater chance of being selected - as suggested by the name @weighteddiv.
      use List::Util 'shuffle'; use List::MoreUtils 'uniq'; use strict; my @weighteddiv = ( (1..50), (1..10) x 10, ); my @shuffle1 = shuffle( uniq @weighteddiv ); my @shuffle2 = uniq( shuffle @weighteddiv ); printf "shuffle1: %s\n", join(', ', sort {$a <=> $b} @shuffle1[0..10]) +; printf "shuffle2: %s\n", join(', ', sort {$a <=> $b} @shuffle2[0..10]) +;
      Output:
      shuffle1: 2, 5, 16, 17, 23, 25, 26, 27, 39, 42, 43 shuffle2: 1, 3, 4, 5, 6, 8, 9, 10, 13, 40, 50
      This approach is appropriate for the current implementation of uniq, which preserves the original order while removing duplicates.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://618804]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found