in reply to removing element from an array
" I know there are half a million posts similar to this one but they aren't very helpful in what I'm trying to do..."
Might be a coincidence, but there are half a million [minus a quarter million or two] posts similar to this one that aren't very helpful in terms of conveying exactly what you are trying to do. The code you have posted is really just the synopsis for Algorithm::Numerical::Shuffle, it is not code that demonstrates why you want to remove an element from the randomized array, and then randomize that array again (and presumably continue this in loop until all elements are exhausted). This why, generally speaking, can really effect the number of answers that you will receive.Since you have no why, i now have to employ the mythical Acme::PSI::ESP and translate your post:
"Hi, i am using Algorithm::Numerical::Shuffle to sort an array. I want to consume elements (like a queue) from this randomized array, but i want to make sure that i only consume unique elements."
If it's really unique elements that you are after, the Perl Cookbook offers a pretty slick snippet in Recipe 4.6. Extracting Unique Elements from a List:So, using that code, we can extract the unique elements before we randomize:%seen = (); @uniqu = grep { ! $seen{$_} ++ } @list;
While i was writing this post, i thought about encapsulating this 'unique' code into a subroutine. Here are a couple of versions, both callable like:use strict; use warnings; use Algorithm::Numerical::Shuffle qw(shuffle); my @array = (1..10,2..11,3..12); print " begin: @array\n"; my %seen; my @unique = grep {!$seen{$_}++} @array; print " unique: @unique\n"; my @shuffled = shuffle @unique; print "shuffled: @shuffled\n";
my @unique = unique(@array);
Version Two: (make ears bleed)sub unique { my %seen; my @unique = grep {!$seen{$_}++} @_; return @unique; }
sub unique { local %_; grep !$_{$_}++, @_ }
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: removing element from an array
by waswas-fng (Curate) on Jun 07, 2003 at 01:45 UTC |