On Tue, Mar 30, 2010 at 9:16 AM, Derek Cannon
<novellterminator / gmail.com> wrote:
> Hello, everyone! What would be the quickest/best way to check elements
> of an array, which themselves are arrays of hashes? Quick example:
>
> mov1 = {"title"=>"Batman Returns", "genre"=>"Action"}
> mov2 = {"title"=>"Batman", "genre"=>"Action"}
> mov3 = {"title"=>"Batman Begins", "genre"=>"Action"}
> mov4 = {"title"=>"The Dark Knight", "genre"=>"Action"}
> array = mov1, mov2, mov3, mov4
>
> Right now, to search for "Batman Beings", for instance, I'm using:
>
> puts array.each { |i|
>  ۢ  >  
>      >  
>  
>
> Is there a better or faster way to do this in Ruby? I'm going to be
> looking through much more data then this, so I want the most efficient
> code possible. I'm just a beginner, so this is pretty much all I know
> how to do in Ruby.

item = array.find {|movie| movie["title"] == "Batman Begins"}

If the array is not sorted by the criteria you want to search for, you
will have to traverse the array looking at all elements (O(n)). If you
have a relevant amount of data you should look into using a real
database to do the searching for you with appropriate indexes.

Jesus.