I know how to randomize an array 31461. I know how to splice elements among arrays.

What I don't know is how to combine the two, such that I shuffle items in array A among all possible other arrays. e.g. I randomly assign where items in array A go (from A into A, B, C ... N), while maintaining the size of A, B, C, ... N.

Here is how I have been thinking about it thus far:

use strict; use warnings; 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); my @new_a = (); my @new_b = (); my @new_c = (); my @sizes = (10, 20, 5); #sizes of array obtained elsewhere my @where_to = ('a', 'b', 'c'); foreach my $move_to (@where_to) { my $size = shift @sizes; my @moving = (); for (1 .. $size) { my $index = int rand @where_to; my $element = $where_to[$index]; push @moving, $element; } print "@moving\n"; foreach my $moving (@moving) { # my $moved = shift @array_$move_to; #I am not sure how to do this # push @new_$moving, $moved; #Again now sure how to do this #But even lacking the correct bit of code here, you can see, # where I am going with this. } }
The problem with this is that it moves to many or not enough items into A B and C. So, is there a way to keep the sizes of the arrays same, but shuffle the items as I have tried to do above?

Thanks for any help the esteemed monks can give,
-Bio.

---- Even a blind squirrel finds a nut sometimes.

In reply to Randomize elements among several arrays, while maintaining original array sizes. by BioNrd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.