[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Repacking an array of arrays

Kaps Lok

7/10/2007 3:01:00 AM

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.

--
Posted via http://www.ruby-....

12 Answers

dblack

7/10/2007 3:11:00 AM

0

yermej

7/10/2007 3:54:00 AM

0

On Jul 9, 10:00 pm, Kaps Lok <jocub...@gmail.com> 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.

Is the fact that it goes from 3 of 5 to 5 of 3 significant or do you
always want to go to some number of 3 element arrays? E.g. would you
ever have an array of 4 arrays of 6 elements and want to go to an
array of 6 arrays with 4 elements each?

If it's the first case, this will work, though there may be something
more elegant:

result = []
a.flatten.each_with_index {|x, i| i % 3 == 0 ? result << [x] :
result[-1] << x}

I suppose, if you need the second case, you could do something a
little more general:

result = []
a.flatten.each_with_index {|x, i| i % a.length == 0 ? result << [x] :
result[-1] << x}

Jeremy

Ken Bloom

7/10/2007 3:58:00 AM

0

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...

Kaps Lok

7/10/2007 4:14:00 AM

0

unknown wrote:
> If you have ActiveSupport you can do:
>
> array.flatten.in_groups_of(3)
>
> David

Thanks again David.

--
Posted via http://www.ruby-....

Robert Klemme

7/10/2007 8:17:00 AM

0

2007/7/10, Kaps Lok <jocubeit@gmail.com>:
> 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.

Try this:

require 'enumerator'
p [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12,
15]].flatten.to_enum(:each_slice, 3).to_a

Kind regards

robert

SonOfLilit

7/10/2007 8:58:00 AM

0

a = [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]]

a0 = a.shift

p a0.zip(*a)


to grok this, ri Array#zip

On 7/10/07, Kaps Lok <jocubeit@gmail.com> 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.
>
> --
> Posted via http://www.ruby-....
>
>

Robert Klemme

7/10/2007 9:43:00 AM

0

2007/7/10, SonOfLilit <sonoflilit@gmail.com>:
> a = [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]]
>
> a0 = a.shift
>
> p a0.zip(*a)

This outputs

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]

Which is not what the OP wanted according to his first posting (see
quote below). Still it's a nice approach!

> to grok this, ri Array#zip
>
> On 7/10/07, Kaps Lok <jocubeit@gmail.com> 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.

Kind regards

robert

dblack

7/10/2007 12:27:00 PM

0

James Gray

7/10/2007 12:44:00 PM

0

On Jul 10, 2007, at 7:26 AM, dblack@wobblini.net wrote:

> Hi --
>
> On Tue, 10 Jul 2007, Robert Klemme wrote:
>
>> 2007/7/10, Kaps Lok <jocubeit@gmail.com>:
>>> 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.
>>
>> Try this:
>>
>> require 'enumerator'
>> p [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12,
>> 15]].flatten.to_enum(:each_slice, 3).to_a
>
> And in 1.9 I guess it would be:
>
> array.flatten.each_slice(3).to_a
>
> I'd like to see a method (I don't think there is one) in 1.9 that
> would return the sliced-up array without the need for the explict to_a
> operation. I suspect the most common use will be exactly that.

I bet rolling …_with_index iterators will be pretty common too:

enum.each_with_index.inject…


James Edward Gray II

SonOfLilit

7/10/2007 1:17:00 PM

0

Oi...

How could I have misread so badly?

I'm sorry, I had this so cool elegant hammer and I guess I immediately
saw a nail :P


Aur

On 7/10/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> 2007/7/10, SonOfLilit <sonoflilit@gmail.com>:
> > a = [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]]
> >
> > a0 = a.shift
> >
> > p a0.zip(*a)
>
> This outputs
>
> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
>
> Which is not what the OP wanted according to his first posting (see
> quote below). Still it's a nice approach!
>
> > to grok this, ri Array#zip
> >
> > On 7/10/07, Kaps Lok <jocubeit@gmail.com> 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.
>
> Kind regards
>
> robert
>
>