Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: List into two-dimensional array

by haukex (Archbishop)
on Dec 14, 2020 at 18:25 UTC ( [id://11125179]=note: print w/replies, xml ) Need Help??


in reply to List into two-dimensional array

use warnings; use strict; use Test::More tests => 2; my ($cols,$rows) = (4,3); my @list = (1..12); my @AoA = map { [ @list[ $_*$cols .. ($_+1)*$cols-1 ] ] } 0..$rows-1; is_deeply \@AoA, [[1 .. 4], [5 .. 8], [9 .. 12]] or diag explain \@AoA; is_deeply \@list, [1..12];

TIMTOWTDI, plenty of other solutions are of course possible. Update: This one assumes that your list fits exactly into the rows and cols that you appear to know beforehand, at least based on your example code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found