Group array elements in groups of two

A better way to do this? :

arr= [1, 2, 3, 4, 5, 6, 7, 8]
new= []
while !arr.empty?
elem1, elem2= arr.pop, arr.pop
new << [elem2, elem1];
end
new.reverse!

new= [[1, 2], [3, 4], [5, 6], [7, 8]]

Thanks!

Alle lunedì 17 settembre 2007, Emmanuel O. ha scritto:

new= [[1, 2], [3, 4], [5, 6], [7, 8]]

Thanks!

Use each_slice:

require ‘enumerator’
arr = [1, 2, 3, 4, 5, 6, 7, 8]
new = []
arr.each_slice(2){|s| new << s}

or each_slice, enum_for an inject:

require ‘enumerator’
arr = [1, 2, 3, 4, 5, 6, 7, 8]
new = arr.enum_for(:each_slice , 2).inject([]){|res, c| res << c}

I hope this helps

Stefano

On 17/09/2007, Emmanuel O. [email protected] wrote:

new= [[1, 2], [3, 4], [5, 6], [7, 8]]

Thanks!
irb(main):012:0> require ‘enumerator’
=> true
irb(main):013:0> new = []
=> []
irb(main):014:0> [1,2,3,4,5,6].each_slice(2) do |slice|
irb(main):015:1* new << slice
irb(main):016:1> end
=> nil
irb(main):017:0> new
=> [[1, 2], [3, 4], [5, 6]]

On Sep 17, 2007, at 10:18 AM, Emmanuel O. wrote:

new= [[1, 2], [3, 4], [5, 6], [7, 8]]

a = (1…8).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8]

require “enumerator”
=> true

a.enum_slice(2).to_a
=> [[1, 2], [3, 4], [5, 6], [7, 8]]

Hope that helps.

James Edward G. II

On Sep 17, 10:18 am, Emmanuel O. [email protected]
wrote:

new= [[1, 2], [3, 4], [5, 6], [7, 8]]
This question has come up several times.

require ‘enumerator’
==>true
[1, 2, 3, 4, 5, 6, 7, 8].enum_slice(2).to_a
==>[[1, 2], [3, 4], [5, 6], [7, 8]]

Without ‘require’:

f=nil
==>nil
[1, 2, 3, 4, 5, 6, 7, 8].partition{f=!f}.transpose
==>[[1, 2], [3, 4], [5, 6], [7, 8]]

very usefull. Thank you!

Hi,

In message “Re: group array elements in groups of two”
on Tue, 18 Sep 2007 00:18:09 +0900, Emmanuel O.
[email protected] writes:

|A better way to do this? :
|
|arr= [1, 2, 3, 4, 5, 6, 7, 8]
|new= []
|while !arr.empty?
| elem1, elem2= arr.pop, arr.pop
| new << [elem2, elem1];
|end
|new.reverse!
|
|new= [[1, 2], [3, 4], [5, 6], [7, 8]]

require ‘enumerator’
arr= [1, 2, 3, 4, 5, 6, 7, 8]
new=arr.to_enum(:each_slice, 2).to_a

Stefano C. wrote:

require ‘enumerator’
arr = [1, 2, 3, 4, 5, 6, 7, 8]
new = arr.enum_for(:each_slice , 2).inject([]){|res, c| res << c}

You can simply replace the inject with a call to to_a:
new = arr.enum_for(:each_slice , 2).to_a

On Sep 17, 10:35 am, Yukihiro M. [email protected] wrote:

| elem1, elem2= arr.pop, arr.pop
| new << [elem2, elem1];
|end
|new.reverse!
|
|new= [[1, 2], [3, 4], [5, 6], [7, 8]]

require ‘enumerator’
arr= [1, 2, 3, 4, 5, 6, 7, 8]
new=arr.to_enum(:each_slice, 2).to_a

Nirvana at last! I won a round of golf with
Matz!

YM: .to_enum(:each_slice, 2).to_a
WJ: .enum_slice(2).to_a

On Sep 17, 10:07 am, William J. [email protected] wrote:

Nirvana at last! I won a round of golf with
Matz!

YM: .to_enum(:each_slice, 2).to_a
WJ: .enum_slice(2).to_a

Perhaps, but you both sliced your shots.

Hi,

In message “Re: group array elements in groups of two”
on Tue, 18 Sep 2007 01:10:10 +0900, William J.
[email protected] writes:

|Nirvana at last! I won a round of golf with
|Matz!
|
|YM: .to_enum(:each_slice, 2).to_a
|WJ: .enum_slice(2).to_a

Well done, but your version does not work on 1.8. :wink:

          matz.

Yukihiro M. wrote:

Well done, but your version does not work on 1.8. :wink:

          matz.

Will the world stop turning or am i just trapped in a parallel universe?

$ ruby -v -renumerator -e “p [1,2,3,4,5,6,7,8,9,0].enum_slice(2).to_a”
ruby 1.8.4 (2005-12-24) [i386-cygwin]
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]

cheers

Simon

William J. wrote:

[…]
Nirvana at last! I won a round of golf with
Matz!

YM: new=arr.to_enum(:each_slice, 2).to_a
WJ: new=arr.enum_slice(2).to_a
SK: new=*arr.enum_slice(2)

cheers

Simon