# Searching an array

**URL:** <https://www.ruby-forum.com/t/searching-an-array/181971>\
**Category:** Rails\
**Created:** [January 21, 2010, 12:51am UTC](https://www.ruby-forum.com/t/searching-an-array/181971 "2010-01-21T00:51:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 21, 2010, 12:51am UTC](https://www.ruby-forum.com/t/searching-an-array/181971/1 "2010-01-21T00:51:08Z")

</div>

Hi everyone! 🙂

I’m currently accomplishing what I want, but I’d like to clean it up a  
bit. I’ve placed the following items in an array (please excuse my lack  
of proper syntax):

@widgets =

[1, red]  
[2, blue]  
[3, red]  
[4, blue]

I’d like to learn a way to search through the @widgets variable an  
separate the widgets, so that I could have:

@blue\_widgets =

[2, blue]  
[4, blue]

@red\_widgets =

[1, red]  
[4, blue]

…after performing a query on @widgets.

I’m accomplishing this now by doing multiple queries to my database, but  
I’m sure there’s a way to do it by just doing one query, and then  
separating the data into different variables in my controller. Would  
that be the best place to do this?

As always, any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 21, 2010, 12:52am UTC](https://www.ruby-forum.com/t/searching-an-array/181971/2 "2010-01-21T00:52:01Z")

</div>

Steve C. wrote:

> @red\_widgets =
> 
> [1, red]  
> [4, red]

Fixed the above - should of said “red”.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 21, 2010, 12:54am UTC](https://www.ruby-forum.com/t/searching-an-array/181971/3 "2010-01-21T00:54:32Z")

</div>

Steve C. wrote:

> Hi everyone! 🙂
> 
> I’m currently accomplishing what I want, but I’d like to clean it up a  
> bit. I’ve placed the following items in an array (please excuse my lack  
> of proper syntax):
> 
> @widgets =
> 
> [1, red]  
> [2, blue]  
> [3, red]  
> [4, blue]
> 
> I’d like to learn a way to search through the @widgets variable an  
> separate the widgets, so that I could have:
> 
> @blue\_widgets =
> 
> [2, blue]  
> [4, blue]
> 
> @red\_widgets =
> 
> [1, red]  
> [4, blue]
> 
> …after performing a query on @widgets.
> 
> I’m accomplishing this now by doing multiple queries to my database, but  
> I’m sure there’s a way to do it by just doing one query, and then  
> separating the data

Look at Array#partition.

> into different variables in my controller. Would  
> that be the best place to do this?

It might be better in the model. You shouldn’t have much logic in the  
controller.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 21, 2010, 12:59am UTC](https://www.ruby-forum.com/t/searching-an-array/181971/4 "2010-01-21T00:59:36Z")

</div>

Marnen Laibow-Koser wrote:

> Look at Array#partition.
> 
> > into different variables in my controller. Would  
> > that be the best place to do this?
> 
> It might be better in the model. You shouldn’t have much logic in the  
> controller.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Thanks as always for the guidance, Marnen. I’m checking that out now.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 21, 2010, 2:57pm UTC](https://www.ruby-forum.com/t/searching-an-array/181971/5 "2010-01-21T14:57:39Z")

</div>

On Jan 20, 2010, at 6:59 PM, Steve C. wrote:

> > ## Best,
> > 
> > Marnen Laibow-Koser  
> > [http://www.marnen.org](http://www.marnen.org)  
> > removed\_email\_address@domain.invalid
> 
> Thanks as always for the guidance, Marnen. I’m checking that out now.

And if you have more than just red/blue (or any two-choice condition),  
you might have more success with Enumerable#group\_by (as added by  
ActiveSupport). Where Array#partition gives you a two-element array,  
Enumerable#group\_by gives you a hash with the “condition” as the key  
and an array of elements as the value.

irb\> require ‘rubygems’  
=\> true  
irb\> require ‘activesupport’  
=\> true  
irb\> widgets = [[1, ‘red’], [2, ‘blue’], [3, ‘red’], [4, ‘blue’], ]  
=\> [[1, “red”], [2, “blue”], [3, “red”], [4, “blue”]]  
irb\> widgets.partition {|number,color| color == ‘blue’ }  
=\> [[[2, “blue”], [4, “blue”]], [[1, “red”], [3, “red”]]]  
irb\> widgets.group\_by {|number, color| color }  
=\> #\<OrderedHash {“blue”=\>[[2, “blue”], [4, “blue”]], “red”=\>[[1,  
“red”], [3, “red”]]}\>

(OK, so you actually get an OrderedHash from ruby1.8.6, but it works  
like you’d expect.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
