Yes but ref aliasing doesn't allow slicing a subset out of it.

Yes, this is true. But earlier you said you would abstract it out into a function anyway, so it doesn't really matter how it's implemented ;-)

<update> See also </update>

use warnings; use strict; sub arr_alias_1 { my ($arr,@i) = @_; return sub {\@_}->(@$arr[@i]); } use Data::Alias 'alias'; sub arr_alias_2 { my ($arr,@i) = @_; alias my @x = @$arr[@i]; return \@x; } sub arr_alias_3 { my ($arr,@i) = @_; use 5.022; # for refaliasing use experimental 'refaliasing'; my @x; #(\@x) = \(@$arr[@i]); # Assigned value is not an ARRAY reference #\@x[0..$#i] = \(@$arr[@i]); # @x is just undefs? \$x[@x] = \$$arr[$_] for @i; # works return \@x; } sub hash_alias_1 { my ($hash,@k) = @_; ... # not possible in pure Perl? } use Data::Alias 'alias'; sub hash_alias_2 { my ($hash,@k) = @_; use 5.020; # for Key/Value Hash Slices alias my %x = %$hash{@k}; return \%x; } sub hash_alias_3 { my ($hash,@k) = @_; use 5.022; # for refaliasing use experimental 'refaliasing'; my %x; #\(@x{@k}) = \(@$hash{@k}); # values of %x are undefs? \$x{$_} = \$$hash{$_} for @k; # works return \%x; } # arr_alias_4 and hash_alias_4 could be implemented with "tie"... use Test::More tests=>10; for my $aa (\&arr_alias_1,\&arr_alias_2,\&arr_alias_3) { my @arr = ('a'..'z'); my $subset = $aa->(\@arr, 5..10); is_deeply $subset, ['f'..'k'] or diag explain $subset; $subset->[3] = 9; is_deeply \@arr, ['a'..'h',9,'j'..'z']; } for my $ha (\&hash_alias_2,\&hash_alias_3) { my %hash = map {$_=>uc} 'a'..'z'; my $subset = $ha->(\%hash, 'f'..'k'); is_deeply $subset, { map {$_=>uc} 'f'..'k' } or diag explain $subset; $subset->{i} = 9; is_deeply \%hash, { i=>9, map {$_=>uc} 'a'..'h','j'..'z' }; }

In reply to Re^8: Selecting Ranges of 2-Dimensional Data by haukex
in thread Selecting Ranges of 2-Dimensional Data by haukex

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.