group array elements in groups of two

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

  1. Emmanuel Oga

    Emmanuel Oga Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
     
    Stefano Crocco, Sep 17, 2007
    #3
  4. 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
    #4
  5. 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
    #5
  6. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 17, 2007
    #6
  7. Emmanuel Oga

    Emmanuel Oga Guest

    Emmanuel Oga, Sep 17, 2007
    #7
  8. 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_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
     
    William James, Sep 17, 2007
    #8
  9. Emmanuel Oga

    Phrogz Guest

    On Sep 17, 10:07 am, William James <> 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.
     
    Phrogz, Sep 17, 2007
    #9
  10. 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
     
    Simon Kröger, Sep 17, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Petra Hübner
    Replies:
    0
    Views:
    471
    Petra Hübner
    Feb 16, 2004
  2. anonymous
    Replies:
    1
    Views:
    4,665
    Francisco Padron
    May 8, 2005
  3. Son KwonNam
    Replies:
    1
    Views:
    926
    Dimitre Novatchev
    Oct 14, 2003
  4. Replies:
    3
    Views:
    383
    Peter Hansen
    Jun 10, 2005
  5. Replies:
    18
    Views:
    1,124
    Tomi Häsä
    Jan 22, 2005
Loading...

Share This Page