Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: List into two-dimensional array

by johngg (Canon)
on Dec 15, 2020 at 11:18 UTC ( [id://11125225]=note: print w/replies, xml ) Need Help??


in reply to List into two-dimensional array

You could use an on-the-fly subroutine. Probably not a good approach if the arrays are large.

johngg@abouriou:~$ perl -Mstrict -Mwarnings -MData::Dumper -E ' my @list = qw{ 1 2 3 4 5 6 7 8 9 10 11 12 }; my( $nCols, $nRows ) = ( 4, 3 ); my @AoA = sub { my @tAoA; push @tAoA, [ splice @_, 0, $nCols ] while @_; return @tAoA; }->( @list ); print Data::Dumper->Dumpxs( [ \ @list, \ @AoA ], [ qw{ *list *AoA } ] +);' @list = ( '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' ); @AoA = ( [ '1', '2', '3', '4' ], [ '5', '6', '7', '8' ], [ '9', '10', '11', '12' ] );

I hope this is of interest.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found