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.
RIP an inspiration; A true Folk's Guy

In reply to Re^3: how to alias a subset of an array (sub) by BrowserUk
in thread how to alias a subset of an array by Anonymous Monk

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.