BioNrd has asked for the wisdom of the Perl Monks concerning the following question:
I know this is possible with a splice ($ranpop = splice(@holder, int rand(@holder), 1)); but I do not want to destroy the array.
I was thinking something along these lines:
But I am doing something wrong, and I can not figure it out. Somewhere or something in that last loop needs to be adjusted. Any help is appreciated.use strict; use warnings; my @final; my $item; my @list = (2, 1, 4, 3, 8, 5); #already selected from array #image another loop someplace else that is pushing into @list for (1 .. 5) { #getting 5 numbers not on the list above... my $find = int rand (10); #my "array" I am picking from is 0-10 #below this is my attempt to solve my problem my $found_flag = 0; foreach my $item ( @list ) { if( $item eq $find ) { $found_flag = 1; last; } } if( $found_flag ) { print "Found $find.\n"; until ( $find != $item ) { $find = int rand (10); } } #I tried to basically say # if (the item is already in the array) { # pick a value until it is not found in the array # } push @final, $find; } print "@final \n";
-Bio
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Select value from array w/o replacement (or destroying the array) (indirect shuffle)
by tye (Sage) on Dec 03, 2007 at 03:22 UTC | |
Re: Select value from array w/o replacement (or destroying the array)
by kyle (Abbot) on Dec 03, 2007 at 03:19 UTC | |
Re: Select value from array w/o replacement (or destroying the array)
by tachyon-II (Chaplain) on Dec 03, 2007 at 03:29 UTC | |
Re: Select value from array w/o replacement (or destroying the array)
by Gangabass (Vicar) on Dec 03, 2007 at 03:29 UTC | |
Re: Select value from array w/o replacement (or destroying the array)
by BioNrd (Monk) on Dec 03, 2007 at 03:41 UTC | |
Re: Select value from array w/o replacement (or destroying the array)
by moritz (Cardinal) on Dec 03, 2007 at 10:01 UTC | |
Re: Select value from array w/o replacement (or destroying the array)
by fenLisesi (Priest) on Dec 03, 2007 at 09:33 UTC | |
by BioNrd (Monk) on Dec 04, 2007 at 16:30 UTC |