There are two way of looking at what constitutes a slice of a 2d array--and more for higher dimensions:)

In the first veiw, the result is what you would get if you could notionally extend perl's @arr[n..m] syntax to a 2D array: @arr[*][n..m] which might be described as "Give me a new 2d array where each row consists of just the columns n through m of the original array". For your example array, and with n & m set to 1 & 2 respectively, this would result in

my @vslice = (['b','c'], ['i','j'], ['y','z']);

The second view, and the one you want, is where the resultant arrays row(s) become 1 (or more) column(s) from the original array. for the same example as above you get

my @yxslice = ( ['b','i','y'], ['c','j','z'] );

Two functions to produce these

#! perl -slw use strict; use Data::Dumper; sub vslice { map{ [ @{$_}[@_] ] } @{+shift}; } sub yxslice { my $aref = shift; map{ my $i=$_; [ map{ $_->[$i] } @$aref ] } @_; } my @arr = (['a','b','c','d'], ['h','i','j','k'], ['w','x','y','z']); my @vslice = vslice \@arr, 1..2; print Dumper \@vslice; my @yxslice = yxslice \@arr, 1..2; print Dumper \@yxslice; __END__ C:\test>240941 $VAR1 = [ [ 'b', 'c' ], [ 'i', 'j' ], [ 'x', 'y' ] ]; $VAR1 = [ [ 'b', 'i', 'x' ], [ 'c', 'j', 'y' ] ];

I would use these versions of the two functions as it simplifies the syntax of using them slightly, but prototypes are almost universally condemned.

sub vslice (\@@) { map{ [@{$_}[@_]] } @{+shift}; } sub yxslice (\@@) { my $aref = shift; map{ my $i=$_; [ map{ $_->[$i] } @$aref ] } @_; }

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: Slice of a multidimensional array by BrowserUk
in thread Slice of a multidimensional 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.