From: Ken Bloom Date: 2007-07-11T22:20:20+09:00 Subject: Re: Repacking an array of arrays On Tue, 10 Jul 2007 12:00:47 +0900, Kaps Lok wrote: > Is there an elegant (maybe a one-liner) for repacking: > > [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]] > > to? > > [[1, 4, 7], [10, 13, 2], [5, 8, 11], [14, 3, 6], [9, 12, 15]] > > So rather than having 3 arrays each with 5 elements, I get five arrays > of 3 elements each... > > the only way I can see to do this is by looping the array, and creating > new arrays. > > Any help would be much appreciated. require 'enumerator' a=[[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]] a.flatten.enum_slice(3).collect => [[1, 4, 7], [10, 13, 2], [5, 8, 11], [14, 3, 6], [9, 12, 15]] -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/