Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Rosetta code: Split an array into chunks

by eyepopslikeamosquito (Chancellor)
on Sep 25, 2010 at 09:29 UTC ( #861938=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download

  1. or download this
    a bb c
    d e f
    g h
  2. or download this
    use strict;
    use warnings;
    my $v2 = chunk_array(3, "a", "bb", "c", "d", "e", "f");
    $v2 eq "a bb c\nd e f\n" or die "error: '$v2'\n";
    print $v2;
  3. or download this
    use List::MoreUtils qw(part natatime);
        my $i = 0;
        return join "", map { join(" ", @$_)."\n" } part { $i++/$n } @vals
  4. or download this
    from itertools import *
    def chunk_array(n, vals):
      return "".join(" ".join(x for x in i if x!=None)+"\n" for i in group
    +(n, vals))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://861938]
Approved by GrandFather
Front-paged by moritz
[Corion]: DST does that to everybody...
Corion attempts to find who to address to better target his drive-by patches
[Corion]: This time it's for the Duktape embedded Javascript engine, which would be nice to have IMO, but doesn't compile on Windows currently
[Corion]: I imagine it would be fun to have WWW::Mechanize:: Duktape, but I haven't seen a web browser written in Javascript yet, except the ones distributed with PhantomJs

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-03-27 11:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (319 votes). Check out past polls.