Update: This matches the OPs algorithm:
#! perl -slw use strict; use List::Util qw[ shuffle ]; my @cols = shuffle qw/0 1 2 3 4 5 6 7 8 9 10 11/; my @nums = map { [qw/01 02 03 04 05 06 07 08 09 10 11 12/], } 1 .. 12; my @swapped = map{ [ @{ $_ }[ @cols[ 6 .. 11 ] ], @{ $_ }[ @cols[ 0 .. 5 ] ] ] } @nums; print "@$_" for @swapped; __END__ P:\test>467617 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09
Update2: And that can be improved (made clearer (IMO)):
#! perl -slw use strict; use List::Util qw[ shuffle ]; my @cols = shuffle 0 .. 11; my @nums = map { [ '01' .. '12' ] } 1 .. 12; my @swapped = map{ [ @{ $_ }[ @cols[ 6 .. 11 ] ], @{ $_ }[ @cols[ 0 .. 5 ] ] ] } @nums; print "@$_" for @swapped; __END__ P:\test>467617 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09 07 04 08 05 03 02 12 10 01 11 06 09
Don't!
A question mark regarding your spec is the bit about "2) each column must be swapped at least once.". That could be interpreted as "No column shall remain in it's original position"? Neither your nor my solution would meet that criteria.
This is easier on the eyes and should be a little more efficient than all those c-style loops.
#! perl -slw use strict; use List::Util qw[ shuffle ]; my @cols = shuffle qw/0 1 2 3 4 5 6 7 8 9 10 11/; my @nums = map { [qw/01 02 03 04 05 06 07 08 09 10 11 12/], } 1 .. 12; my @swapped = map{ [ @{ $_ }[ @cols ] ] } @nums; print "@$_" for @swapped; __END__ P:\test>467617 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09 12 05 06 04 02 08 10 11 03 01 07 09
In reply to Re: swap columns in a 2-dim array
by BrowserUk
in thread swap columns in a 2-dim array
by davidj
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |