Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: List into two-dimensional array

by johngg (Canon)
on Dec 15, 2020 at 11:18 UTC ( #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? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2023-02-06 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (34 votes). Check out past polls.

    Notices?