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/.
Alle lunedì 17 settembre 2007, Emmanuel Oga ha scritto:
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!
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
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 17/09/2007, Emmanuel Oga <oga_emmanuel_oga@yahoo.com.ar> 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!
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
···
On Sep 17, 2007, at 10:18 AM, Emmanuel Oga wrote:
W_James
(W. James)
5
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]]
···
On Sep 17, 10:18 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> 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]]
Stefano Crocco 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
···
--
NP: The Haunted - DOA
Jabber: sepp2k@jabber.org
ICQ: 205544826
W_James
(W. James)
9
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:35 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote:
Hi,
In message "Re: group array elements in groups of two" > on Tue, 18 Sep 2007 00:18:09 +0900, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> 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
Perhaps, but you both sliced your shots.
···
On Sep 17, 10:07 am, William James <w_a_x_...@yahoo.com> 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
William James 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
Yukihiro Matsumoto wrote:
Hi,
>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. 
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
···
In message "Re: group array elements in groups of two" > on Tue, 18 Sep 2007 01:10:10 +0900, William James <w_a_x_man@yahoo.com> writes: