paco.onrails
2008-Jul-16 22:30 UTC
Building an array which contains arrays grouped by 3 items (in a line)
Hello again. I have an array which contains several items (Array_a) and what I need is create a new array (Array_b) which includes same items of Array_a, but grouped in arrays within 3 items each. For example: Array_a: [''a1'',''a2'',''a3'',''b1'',''b2'',''b3'',''c1'',''c2'',''c3'',''d1'',''d2'',''d3''] After iteration: Array_b: [ [''a1'',''a2'',''a3''],[''b1'',''b2'',''b3''],[''c1'',''c2'',''c3''], [''d1'',''d2'',''d3''] ] Can anybody give me an idea of what iteration can I use to get that? If it could be done in one single line, much better. Thanks a lot. Best! Paco Reyes Interaction Designer www.xentido.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2008-Jul-17 01:39 UTC
Re: Building an array which contains arrays grouped by 3 ite
paco.onrails wrote:> Hello again. > I have an array which contains several items (Array_a) and what I need > is create a new array (Array_b) which includes same items of Array_a, > but grouped in arrays within 3 items each. > > For example: > > Array_a: [''a1'',''a2'',''a3'',''b1'',''b2'',''b3'',''c1'',''c2'',''c3'',''d1'',''d2'',''d3''] > > After iteration: > > Array_b: [ [''a1'',''a2'',''a3''],[''b1'',''b2'',''b3''],[''c1'',''c2'',''c3''], > [''d1'',''d2'',''d3''] ] > > Can anybody give me an idea of what iteration can I use to get that? > If it could be done in one single line, much better. Thanks a lot. > > Best! > > Paco Reyes > Interaction Designer > www.xentido.comHi Paco, Take a look at in_groups_of. http://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M001093&name=in_groups_of Peace, Phillip -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---