in reply to Re: Randomize elements among several arrays, while maintaining original array sizes.
in thread Randomize elements among several arrays, while maintaining original array sizes.
The simplest way to do what the OP wanted would be as pjotrik suggested. Your recommendation of List::Util's shuffle is definitely a good suggestion though.
Addendum: A quick and dirty example...
#!/usr/bin/perl -w use strict; use List::Util 'shuffle'; my @array_a = (10, 22, 34, 21, 20, 12, 32, 31, 91, 20); my @array_b = (10, 22, 34, 21, 20, 12, 32, 31, 91, 20, 11, 32, 44, 51, + 10, 22, 82, 71, 61, 54); my @array_c = (14, 25, 64, 71, 80); mass_shuffle(\@array_a, \@array_b, \@array_c); sub mass_shuffle { # all sub-arrays passed by ref my @arrays = @_; # combine into one large array and shuffle it my @shuffler; foreach my $sub (@arrays) { push @shuffler, @{$sub}; } @shuffler = shuffle(@shuffler); # splice properly sized randomized replacements foreach my $sub (@arrays) { @{$sub} = splice @shuffler, 0, scalar(@{$sub}); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Randomize elements among several arrays, while maintaining original array sizes.
by BioNrd (Monk) on Aug 01, 2008 at 16:50 UTC |