in reply to swap columns in a 2-dim array
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
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: swap columns in a 2-dim array
by polettix (Vicar) on Jun 17, 2005 at 10:43 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2005 at 10:55 UTC | |
by polettix (Vicar) on Jun 17, 2005 at 12:26 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2005 at 13:08 UTC |
In Section
Seekers of Perl Wisdom