♪..♫ It's a kind of magic ... ♩..♬ ;-)
use warnings; use strict; #use Data::Dump qw/pp/; use Test::More tests=>2; sub arr_alias { \ @_ } # arr_ref to list of aliases sub getsubset { my ( $data, $range ) = @_; my ( $rows, $cols ) = @$range; # [y0,y1], [x0,x1] # YMMV! return [ map { arr_alias @$_[ $cols->[0] .. $cols->[1] ] # x-slice } @$data[ $rows->[0] .. $rows->[1] ] # y-slice ]; } my $data = [ ['a','b','c','d'], ['e','f','g','h'], ['i','j','k','l'], ['m','n','o','p'] ]; my $range = [ [1,2], [1,2] ]; # or any other useful syntax! my $subset = getsubset($data, $range); is_deeply $subset, [ ['f','g'], ['j','k'] ]; # Part 2: $subset->[0][1] = 'X'; $subset->[1][0] = 'Y'; is_deeply $data, [ ['a','b','c','d'], ['e','f','X','h'], ['i','Y','k','l'], ['m','n','o','p'] ];

C:/Perl_524/bin\perl.exe d:/tmp/matrix_range.pl 1..2 ok 1 ok 2

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

simplified code, added comments

update

changed from slices to ranges


In reply to Re: Selecting Ranges of 2-Dimensional Data (array of aliases) by LanX
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.