From: "Jesús Gabriel y Galán" Date: 2008-09-22T22:25:32+09:00 Subject: Re: Merging two arrays On Mon, Sep 22, 2008 at 3:02 PM, Chris Chris wrote: > Hi, > > in Ruby, I have two arrays: > > 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"] I assume you mean you want an array of three entries like the above one: irb(main):017:0> products.map {|x| x + links.assoc(x[1])[1..-1]} => [["Amazon", 121212, "Harry Potter", "www.amazon.com/abc"], ["Amazon", 242424, "John Grisham", "www.amazon.com/johngrisham"], ["Amazon", 353535, "Michael Crichton", "www.amazon.com/somelink"]] The method assumes that in the products array, the id is in index 1, and in the links array it's in index 0. Anyway, if you need to do many operations based on the id I suggest using a hash with the id as the key and an array (or Struct) as the value. Hope this helps, Jesus.