Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: stack array to array of arrays

by johngg (Canon)
on Oct 16, 2013 at 21:54 UTC ( [id://1058561]=note: print w/replies, xml ) Need Help??


in reply to stack array to array of arrays

Using a subroutine that takes a code block as its first argument and applies that code to groups of size specified in the second argument taken from the remaining arguments.

use strict; use warnings; use Data::Dumper; sub groupsOf (&$@); my $doubleStack = [ groupsOf { [ @_ ] } 3, qw{ a b c e 1 2 3 4 5 3 f } ]; print Data::Dumper ->Dumpxs( [ $doubleStack ], [ qw{ doubleStack } ] ); sub groupsOf (&$@) { my $rcToRun = shift; my $groupsOf = shift; my $rcDoIt; $rcDoIt = sub { $rcToRun->( map shift, 1 .. ( @_ < $groupsOf ? @_ : $groupsOf ) ), @_ ? &$rcDoIt : (); }; &$rcDoIt; }

The output.

$doubleStack = [ [ 'a', 'b', 'c' ], [ 'e', '1', '2' ], [ '3', '4', '5' ], [ '3', 'f' ] ];

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://1058561]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found