# group array elements in groups of two

Discussion in 'Ruby' started by Emmanuel Oga, Sep 17, 2007.

1. ### Emmanuel OgaGuest

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!
--
Posted via http://www.ruby-forum.com/.

Emmanuel Oga, Sep 17, 2007

2. ### William JamesGuest

On Sep 17, 10:18 am, Emmanuel Oga <>
wrote:
> 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]]

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

William James, Sep 17, 2007

3. ### Stefano CroccoGuest

Alle luned=C3=AC 17 settembre 2007, Emmanuel Oga ha scritto:
> A better way to do this? :
>
> arr=3D [1, 2, 3, 4, 5, 6, 7, 8]
> new=3D []
> while !arr.empty?
> elem1, elem2=3D arr.pop, arr.pop
> new << [elem2, elem1];
> end
> new.reverse!
>
> new=3D [[1, 2], [3, 4], [5, 6], [7, 8]]
>
> Thanks!

Use each_slice:
=20
require 'enumerator'
arr =3D [1, 2, 3, 4, 5, 6, 7, 8]
new =3D []
arr.each_slice(2){|s| new << s}

or each_slice, enum_for an inject:

require 'enumerator'
arr =3D [1, 2, 3, 4, 5, 6, 7, 8]
new =3D arr.enum_foreach_slice , 2).inject([]){|res, c| res << c}

I hope this helps

Stefano

Stefano Crocco, Sep 17, 2007
4. ### Farrel LifsonGuest

On 17/09/2007, Emmanuel Oga <> wrote:
> 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!

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

Farrel Lifson, Sep 17, 2007
5. ### James Edward Gray IIGuest

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

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

>> 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 Gray II

James Edward Gray II, Sep 17, 2007
6. ### Sebastian HungereckerGuest

Stefano Crocco wrote:
> require 'enumerator'
> arr = [1, 2, 3, 4, 5, 6, 7, 8]
> new = arr.enum_foreach_slice , 2).inject([]){|res, c| res << c}

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

--
NP: The Haunted - DOA
Jabber:
ICQ: 205544826

Sebastian Hungerecker, Sep 17, 2007
7. ### Emmanuel OgaGuest

Emmanuel Oga, Sep 17, 2007
8. ### William JamesGuest

On Sep 17, 10:35 am, Yukihiro Matsumoto <> wrote:
> Hi,
>
> In message "Re: group array elements in groups of two"
> on Tue, 18 Sep 2007 00:18:09 +0900, Emmanuel Oga <> 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_enumeach_slice, 2).to_a

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

YM: .to_enumeach_slice, 2).to_a
WJ: .enum_slice(2).to_a

William James, Sep 17, 2007
9. ### PhrogzGuest

On Sep 17, 10:07 am, William James <> wrote:
> Nirvana at last! I won a round of golf with
> Matz!
>
> YM: .to_enumeach_slice, 2).to_a
> WJ: .enum_slice(2).to_a

Perhaps, but you both sliced your shots.

Phrogz, Sep 17, 2007
10. ### Simon KrögerGuest

William James wrote:
> [...]
> Nirvana at last! I won a round of golf with
> Matz!
>
> YM: new=arr.to_enumeach_slice, 2).to_a
> WJ: new=arr.enum_slice(2).to_a

SK: new=*arr.enum_slice(2)

cheers

Simon

Simon Kröger, Sep 17, 2007