Grouping elements of an array?

Discussion in 'Ruby' started by Sam Kong, Jan 8, 2007.

  1. Sam Kong

    Sam Kong Guest

    Hello,

    What's the best way to do the following?

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

    I want to transform the array into an array of arrays with fixed number
    of elements.
    I think there should be a method but I can't find it.

    I can iterate the elements using a counter to divide but it's so
    boring.

    Thanks in advance.

    Sam
     
    Sam Kong, Jan 8, 2007
    #1
    1. Advertising

  2. class Array
    def group_in(how_many)
    ret_array = []
    new_array = []
    self.each {|elem| how_many.times { new_array << self.shift };
    ret_array << new_array; new_array = []; }
    ret_array
    end
    end

    x = [1,2,3,4,5,6,7,8,9]
    x = x.group_in(3)
    require 'pp'
    pp x

    >> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


    That should do it. :) Not sure if it's the best way, but it will work.

    --Jeremy

    On 1/8/07, Sam Kong <> wrote:
    > Hello,
    >
    > What's the best way to do the following?
    >
    > [1,2,3,4,5,6,7,8,9] => [[1,2,3],[4,5,6],[7,8,9]]
    >
    > I want to transform the array into an array of arrays with fixed number
    > of elements.
    > I think there should be a method but I can't find it.
    >
    > I can iterate the elements using a counter to divide but it's so
    > boring.
    >
    > Thanks in advance.
    >
    > Sam
    >
    >
    >



    --
    My free Ruby e-book:
    http://www.humblelittlerubybook.com/book/

    My blogs:
    http://www.mrneighborly.com/
    http://www.rubyinpractice.com/
     
    Jeremy McAnally, Jan 8, 2007
    #2
    1. Advertising

  3. On 08/01/07, Sam Kong <> wrote:
    > Hello,
    >
    > What's the best way to do the following?
    >
    > [1,2,3,4,5,6,7,8,9] => [[1,2,3],[4,5,6],[7,8,9]]
    >
    > I want to transform the array into an array of arrays with fixed number
    > of elements.
    > I think there should be a method but I can't find it.
    >
    > I can iterate the elements using a counter to divide but it's so
    > boring.
    >
    > Thanks in advance.
    >
    > Sam
    >
    >
    >


    requre 'enumerator'

    [1,2,3,4,5,6,7,8,9].enum_slice(3).inject([]){|array,slice| array << slice}

    Farrel
     
    Farrel Lifson, Jan 8, 2007
    #3
  4. Farrel Lifson wrote:
    > On 08/01/07, Sam Kong <> wrote:
    >> What's the best way to do the following?
    >>
    >> [1,2,3,4,5,6,7,8,9] => [[1,2,3],[4,5,6],[7,8,9]]

    ...
    > [1,2,3,4,5,6,7,8,9].enum_slice(3).inject([]){|array,slice| array << slice}


    Alternately,

    [1,2,3,4,5,6,7,8,9].enum_for:)each_slice,3).to_a
    => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jan 8, 2007
    #4
  5. Sam Kong

    Shiwei Zhang Guest

    FYI. B is what you expect:
    A=[1,2,3,4,5,6,7,8,9];B=[];
    3.times {B<<A.slice!(0..2);}

    Sam Kong wrote:

    >Hello,
    >
    >What's the best way to do the following?
    >
    >[1,2,3,4,5,6,7,8,9] => [[1,2,3],[4,5,6],[7,8,9]]
    >
    >I want to transform the array into an array of arrays with fixed number
    >of elements.
    >I think there should be a method but I can't find it.
    >
    >I can iterate the elements using a counter to divide but it's so
    >boring.
    >
    >Thanks in advance.
    >
    >Sam
    >
    >
    >
    >
     
    Shiwei Zhang, Jan 8, 2007
    #5
  6. Farrel Lifson wrote:
    > On 08/01/07, Sam Kong <> wrote:
    > > Hello,
    > >
    > > What's the best way to do the following?
    > >
    > > [1,2,3,4,5,6,7,8,9] => [[1,2,3],[4,5,6],[7,8,9]]
    > >
    > > I want to transform the array into an array of arrays with fixed number
    > > of elements.
    > > I think there should be a method but I can't find it.
    > >
    > > I can iterate the elements using a counter to divide but it's so
    > > boring.
    > >
    > > Thanks in advance.
    > >
    > > Sam
    > >
    > >
    > >

    >
    > requre 'enumerator'
    >
    > [1,2,3,4,5,6,7,8,9].enum_slice(3).inject([]){|array,slice| array << slice}
    >
    > Farrel


    requre 'enumerator'
    [ 1, 2, 3, 4, 5, 6, 7, 8 ].enum_slice(3).to_aj


    Without "require":
    [ 1, 2, 3, 4, 5, 6, 7, 8 ].inject([[]]){|a,e|
    a << [] if a.last.size==3; a.last << e; a}
     
    William James, Jan 8, 2007
    #6
  7. Sam Kong

    Sam Kong Guest

    Hi William,

    William James wrote:
    > Without "require":
    > [ 1, 2, 3, 4, 5, 6, 7, 8 ].inject([[]]){|a,e|
    > a << [] if a.last.size==3; a.last << e; a}


    This looks really clever.
    I like it.

    Sam
     
    Sam Kong, Jan 8, 2007
    #7
    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. kcwolle
    Replies:
    0
    Views:
    390
    kcwolle
    Nov 27, 2003
  2. Dimitre Novatchev

    .Re: Grouping neighboring elements with xsl

    Dimitre Novatchev, Nov 28, 2003, in forum: XML
    Replies:
    0
    Views:
    501
    Dimitre Novatchev
    Nov 28, 2003
  3. Sam Kong
    Replies:
    5
    Views:
    352
    Peña, Botp
    Oct 22, 2007
  4. Matt Constantine
    Replies:
    2
    Views:
    107
    Robert Klemme
    Sep 22, 2008
  5. Steve Wilhelm

    Grouping elements of an array

    Steve Wilhelm, Mar 18, 2010, in forum: Ruby
    Replies:
    17
    Views:
    253
    Tanaka Akira
    Mar 21, 2010
Loading...

Share This Page