Hello Monks,
I am having a problem shuffling each of the sub-arrays within an array-of-arrays. I'm using the Fisher-Yates shuffle algorithm, as found in 'perldoc -q shuffle'; but I can't get it to work. I've tried three approaches, one using an array of strings, another using an array of array references, and the last using a two dimensional array. None of them work, and I'm not sure why; they just print the contents of the array in the original (unshuffled) order. Can anyone give me some tips?
Thanks in advance!
Rod
my @weeks = ('@week01','@week02','@week03');
my @weeks_refs = ("\@week01","\@week02","\@week03");
my @weeks_arr = ([0..13],[0..11],[0..11]);
my @week01 = (0..13);
my @week02 = (0..11);
my @week03 = (0..11);
for my $ref (@weeks) {
for ($i = @$ref; --$i;) {
my $r = int rand ($i+1);
@$ref[$i, $r] = @$ref[$r, $i];
}
}
for(@weeks){
print;
}
for my $ref (@weeks_refs) {
for ($i = @$ref; --$i;) {
my $r = int rand ($i+1);
@$ref[$i, $r] = @$ref[$r, $i];
}
}
for(@weeks_refs){
print;
}
for my $ref (@weeks_arr) {
for ($i = @$ref; --$i;) {
my $r = int rand ($i+1);
@$ref[$i, $r] = @$ref[$r, $i];
}
}
for(@weeks_arr){
print;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.