From: Henning Bekel Date: 2008-09-22T22:46:57+09:00 Subject: Re: Merging two arrays Chris Chris wrote: > products = Array.new > products << ["Amazon",121212,"Harry Potter"] > products << ["Amazon",242424,"John Grisham"] > products << ["Amazon",353535,"Michael Crichton"] > > links = Array.new > links << [121212,"www.amazon.com/abc"] > links << [242424,"www.amazon.com/johngrisham"] > links << [353535,"www.amazon.com/somelink"] > > I would like to merge these two arrays. The article number is to be > the identifier. > > Thus, I would like an array like this as the result: > ["Amazon",121212,"Harry Potter","www.amazon.com/abc"] products.each do |product| id = product[1] links.each do |link| if link[0] == id product << link[1] end end end This will append the corresponding urls from the "links" array to the individual arrays held in the "products" array. However, since you are already using an id, you might consider using a hash of hashes to store your data, for example: products = { 121212 => { :source => "Amazon", :author => "John Grisham", :url => nil } } Later you can just change the :url value of an identified entry, using products[121212][:url] = "www.amazon.com/johngrisham" This will avoid having your data scattered about over numerous arrays and having to remember which field in which array holds the id value. Using hashes might be more straightforward in this case. Henning