Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Several issues -- first, your slice isn't a slice. You need @sortarr[0..10]. Second, the content of each element is an array reference -- you need to slice out each part of that. p.272 is throwing you off because it's giving a slice of a row -- you're trying for a slice of a column (in a row-major AoA).

You probably want to use map to extract the subslices. E.g.

my @aoa = ( [1 .. 4], [5 .. 8], [9 .. 12], ); my @column_slice = map { [ $_->[2] ] } @aoa[1,2]; my @two_d_slice = map { [ @{$_}[1,2] ] } @aoa[1,2];

For a column slice, for each element of the slice, @aoa[1,2] (i.e. the rows), this dereferences the array reference to get the column, and returns it as a new anonymous array reference. (This keeps it an AoA; if you want a flattened list, omit the square brackets just inside the map braces.). For two-dimensions, it's similar, but you slice inside the map, too.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: Creating a Slice through a 2 dimensional array by xdg
in thread Creating a Slice through a 2 dimensional array by felonius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2023-02-07 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?