Searching an array

Hi everyone! :slight_smile:

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.

Steve Castaneda wrote:

@red_widgets =

[1, red] [4, red]

Fixed the above - should of said "red".

Steve Castaneda wrote:

Hi everyone! :slight_smile:

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 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 marnen@marnen.org

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

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 marnen@marnen.org

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.

require 'rubygems'

=> true

require 'activesupport'

=> true

widgets = [ [1, 'red'], [2, 'blue'], [3, 'red'], [4, 'blue'], ]

=> [[1, "red"], [2, "blue"], [3, "red"], [4, "blue"]]

widgets.partition {|number,color| color == 'blue' }

=> [[[2, "blue"], [4, "blue"]], [[1, "red"], [3, "red"]]]

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com