group array elements in groups of two

E

Emmanuel Oga

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!
 
W

William James

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

Stefano Crocco

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_for:)each_slice , 2).inject([]){|res, c| res << c}

I hope this helps

Stefano
 
F

Farrel Lifson

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

James Edward Gray II

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
 
S

Sebastian Hungerecker

Stefano said:
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
 
W

William James

Hi,

In message "Re: 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]]

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
 
S

Simon Kröger

William said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top