Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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..and easily. I am randomizing an array and I want to remove elements it already used.
use warnings; use strict; use Algorithm::Numerical::Shuffle qw /shuffle/; my @shuffled = shuffle (1, 2, 3, 4, 5, 6, 7); #my $in_situ = [qw /one two three four five six/]; #shuffle $in_situ; print @shuffled;
That randomizes the array perfecty but how do I make it so I can display the first character it finds and remove it so it won't find it next time it's randomized?

Replies are listed 'Best First'.
(jeffa) Re: removing element from an array
by jeffa (Bishop) on Jun 07, 2003 at 00:57 UTC

    " 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:
    %seen = (); @uniqu = grep { ! $seen{$_} ++ } @list;
    So, using that code, we can extract the unique elements before we randomize:
    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";
    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:
    my @unique = unique(@array);
    Hope this helps, and please elaborate if it doesn't. ;)

    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)
    
      Aye jeffa is right on, why would you need to re-randomize between pops? if you have an array, randomize it and use pop to pull items off one at a time the net effect is still returning 1 item in the array randomly with no reuse.

      -Waswas
Re: removing element from an array
by mirod (Canon) on Jun 06, 2003 at 22:13 UTC

    You mean remove the first element of an array? perldoc -f shift? (or are you looking for pop? Or splice?)

Re: removing element from an array
by chanio (Priest) on Jun 07, 2003 at 22:56 UTC
    1st. copy @shuffled to @discards with reverse() (to start by the ending of the queue)

    2nd. print pop(@discards) to: a) get the first element (the last one), and b) to make it dissapear !

    3rd. if you don't want to keep @shuffle you could recover what's left on @discards also with reverse(). And start all over again with one less element.

    Hope it helps