Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a large array and I'd like to get an array reference that points into the original and contains an sees an arbitrary subrange without copying that subrange. In other words, I want the functionality of the following without the copy,

@A = (1,2,3,4,5,7,21,55,66); $B = [ @A[2..4] ];

The contents of @A should be preserved. Ideally, $B->[0] is identical to $A[2].

Thanks!

Replies are listed 'Best First'.
Re: how to alias a subset of an array (sub)
by tye (Sage) on Oct 25, 2010 at 16:09 UTC
    $B= sub { \@_ }->( @A[2..4] );

    Not that I'd recommend doing that.

    - tye        

      I kinda like this solution. What is wrong with it? (why not recommend it?) It is because it depends on sub's aliasing of arguments?

        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.

        No, sub aliasing is well-defined and not going away and so isn't a problem.

        But support for aliases in Perl 5 is very limited and making use of it over long spans will eventually lead to frustration (as well as surprising a lot of people that might try to maintain your code in future). So I certainly don't recommend it. Over short enough of a distance, it might be a reasonable route to take.

        - tye        

Re: how to alias a subset of an array
by JavaFan (Canon) on Oct 25, 2010 at 16:04 UTC
    my @A = (1,2,3,4,5,7,21,55,66); sub TIEARRAY {bless [@_[1..3]],$_[0]} sub FETCH {$_[0][0][$_[1]+$_[0][1]]} tie my @B, "main", \@A, 2, 4; say $B[0]; say $B[1]; say $B[2]; __END__ 3 4 5
    Details like other tie methods, boundary checking, etc, are left as an exercise to the reader.
Re: how to alias a subset of an array
by Arunbear (Prior) on Oct 25, 2010 at 16:07 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Alias; use Data::Dump qw[dd]; my @A = (1,2,3,4,5,7,21,55,66); dd @A; alias my @B = @A[2..4]; dd @B; $B[0] = 0; dd @B; dd @A;
    output:
    +% ~/test/alias.pl (1, 2, 3, 4, 5, 7, 21, 55, 66) (3, 4, 5) (0, 4, 5) (1, 2, 0, 4, 5, 7, 21, 55, 66)

      And for those w32-ites (-ers ?) searching for Data::Alias using ppm; so sorry, not yet, AFAIK.

      But it is, of course on CPAN, or -- as an alternative, this piece of the code therein, for this particular evolution, appears to work when cast as a sub, like so:

      sub alias { use base 'Exporter'; use base 'DynaLoader'; our @EXPORT = qw(alias); our @EXPORT_OK = qw(alias copy deref); our %EXPORT_TAGS = (all => \@EXPORT_OK); pop our @ISA; }

      and called like so: alias(@B);

      which highlights another of my many blindspots! Can someone enlighten me as to how much more of the 400+ line source (mostly POD) can be eliminated or sidestepped? I've read and understood jwkrahn's solution (below) but reading the docs for D::A, Exporter, Dynaloader left me about as clueless as when I began.

      Or is the joke on me because I missed the joke?

        And for those w32-ites (-ers ?)

        ppm install MinGW cpanp i Data::Alias
Re: how to alias a subset of an array
by jwkrahn (Abbot) on Oct 25, 2010 at 16:48 UTC
    $ perl -le' my @A = qw( 1 2 3 4 5 7 21 55 66 ); my @B = \@A[ 2 .. 4 ]; ${ $B[ 0 ] } = 99; print "@A"; ' 1 2 99 4 5 7 21 55 66
Re: how to alias a subset of an array
by Anonymous Monk on Oct 25, 2010 at 15:42 UTC
    I want the functionality of the following without the copy

    You can't have it like that. You might be able to have it like this (see perltie if you want array syntax)

    @A = (1,2,3,4,5,7,21,55,66); $B = RangeRestriction( \@A, [2..4] ); warn $B->(0) ; warn $A[2] ; warn $B->(0) == $A[2] ; sub RangeRestriction { my ( $array, $range ) = @_; return sub { my($index) = @_; $index = $range->[ $index ]; return $array->[ $index ]; }; } __END__ 3 at - line 3. 3 at - line 4. 1 at - line 5.
    But that seems kludgey and doesn't save any memory for simple numbers.
Re: how to alias a subset of an array
by ikegami (Patriarch) on Oct 25, 2010 at 16:38 UTC
    $B = sub { \@_ }->( @A[2..4] );

    [ Oops, tye's answer wasn't there when I opened the page, but that was a while ago ]