I've liked that construct ever since I first saw it here. It makes for some highly efficient solutions to some problems.
For example, in sudoku programs, you have the basic 9x9 grid, but you need to address its contents as rows of 9, columns of 9 and squares of 9 with each element appearing in one of each.
Continually running complex nested loops over the basic board gets expensive. But if you do a little set-up to alias the basic grid in the three different ways, you can quickly check for the existance of particular digits in any one of the views using a simple grep.
And when you insert a value in to any element through any view, it automatically appears in all the others:
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
sub alias{ \@_ }
my $label = 'aa';
my @rows = map{ [ map{ $label++ } 1 .. 9 ] } 1 .. 9;
my @cols;
for my $col ( 0 .. 8 ) {
push @cols, alias(
$rows[ 0 ][ $col ], $rows[ 1 ][ $col ], $rows[ 2 ][ $col ],
$rows[ 3 ][ $col ], $rows[ 4 ][ $col ], $rows[ 5 ][ $col ],
$rows[ 6 ][ $col ], $rows[ 7 ][ $col ], $rows[ 8 ][ $col ],
);
}
my @sqrs;
for my $y ( 0 .. 2 ) {
for my $x ( 0 .. 2 ) {
push @sqrs, alias(
$rows[ $x *3 + 0 ][ $y * 3 + 0 ],
$rows[ $x *3 + 0 ][ $y * 3 + 1 ],
$rows[ $x *3 + 0 ][ $y * 3 + 2 ],
$rows[ $x *3 + 1 ][ $y * 3 + 0 ],
$rows[ $x *3 + 1 ][ $y * 3 + 1 ],
$rows[ $x *3 + 1 ][ $y * 3 + 2 ],
$rows[ $x *3 + 2 ][ $y * 3 + 0 ],
$rows[ $x *3 + 2 ][ $y * 3 + 1 ],
$rows[ $x *3 + 2 ][ $y * 3 + 2 ],
);
}
}
$sqrs[ 4 ][ 4 ] = 9; ## Modify one view
## The change apppears in all views.
pp 'rows', \@rows;
pp 'cols', \@cols;
pp 'sqrs', \@sqrs;
__END__
C:\test>sudoku.pl
(
"rows",
[
["aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai"],
["aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar"],
["as", "at", "au", "av", "aw", "ax", "ay", "az", "ba"],
["bb", "bc", "bd", "be", "bf", "bg", "bh", "bi", "bj"],
["bk", "bl", "bm", "bn", 9, "bp", "bq", "br", "bs"],
["bt", "bu", "bv", "bw", "bx", "by", "bz", "ca", "cb"],
["cc", "cd", "ce", "cf", "cg", "ch", "ci", "cj", "ck"],
["cl", "cm", "cn", "co", "cp", "cq", "cr", "cs", "ct"],
["cu", "cv", "cw", "cx", "cy", "cz", "da", "db", "dc"],
],
)
(
"cols",
[
["aa", "aj", "as", "bb", "bk", "bt", "cc", "cl", "cu"],
["ab", "ak", "at", "bc", "bl", "bu", "cd", "cm", "cv"],
["ac", "al", "au", "bd", "bm", "bv", "ce", "cn", "cw"],
["ad", "am", "av", "be", "bn", "bw", "cf", "co", "cx"],
["ae", "an", "aw", "bf", 9, "bx", "cg", "cp", "cy"],
["af", "ao", "ax", "bg", "bp", "by", "ch", "cq", "cz"],
["ag", "ap", "ay", "bh", "bq", "bz", "ci", "cr", "da"],
["ah", "aq", "az", "bi", "br", "ca", "cj", "cs", "db"],
["ai", "ar", "ba", "bj", "bs", "cb", "ck", "ct", "dc"],
],
)
(
"sqrs",
[
["aa", "ab", "ac", "aj", "ak", "al", "as", "at", "au"],
["bb", "bc", "bd", "bk", "bl", "bm", "bt", "bu", "bv"],
["cc", "cd", "ce", "cl", "cm", "cn", "cu", "cv", "cw"],
["ad", "ae", "af", "am", "an", "ao", "av", "aw", "ax"],
["be", "bf", "bg", "bn", 9, "bp", "bw", "bx", "by"],
["cf", "cg", "ch", "co", "cp", "cq", "cx", "cy", "cz"],
["ag", "ah", "ai", "ap", "aq", "ar", "ay", "az", "ba"],
["bh", "bi", "bj", "bq", "br", "bs", "bz", "ca", "cb"],
["ci", "cj", "ck", "cr", "cs", "ct", "da", "db", "dc"],
],
)
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|