From: Jim Freeze Date: 2005-07-28T23:05:12+09:00 Subject: Re: Combination of two arrays * Brian Schrder [2005-07-28 22:57:18 +0900]: > On 28/07/05, Jim Freeze wrote: > > * Claus Spitzer [2005-07-28 20:57:24 +0900]: > > > > > I have two arrays. let's call them foo and bar. > > > foo = [ "a", "b", "c", ... n ] > > > bar = [ 1 , 2 , 3 , ... m ] > > > > > > I am looking for a way to combine them such that the result is something like > > > > > > [ [ "a", 1 ], [ "a", 2 ], ... [ "a", m ], [ "b", 1 ], ... [ n, 1 ], > > > ... [ n, m ] ] > > > > irb(main):002:0> foo = [ "a", "b", "c" ] > > => ["a", "b", "c"] > > irb(main):003:0> bar = [ 1 , 2 , 3 ] > > => [1, 2, 3] > > irb(main):004:0> foo.zip(bar) > > => [["a", 1], ["b", 2], ["c", 3]] > > > > That is not the same as what the OP was proposing. One could write the > original approach using inject and shorten it one line by that > approach, but that is more obfuscation than neccessary. I don't have > any better proposal for Claus. Oops, read too fast. My apologies. I wrote something just the other day that let me do the following: 5 * [1,2,3] #=> [[5,1], [5,2], [5,3]] "fred" * [1,2,3] #=> [["fred",1], ["fred",2], ["fred",3]] :a * [1,2,3] #=> [[:a,1], [:a,2], [:a,3]] So, I suppost I could take that and do: irb(main):001:0> foo = %w(a b c) => ["a", "b", "c"] irb(main):002:0> bar = [1,2,3] => [1, 2, 3] irb(main):003:0> foo.collect { |f| f * bar } => [[["a", 1], ["a", 2], ["a", 3]], [["b", 1], ["b", 2], ["b", 3]], [["c", 1], ["c", 2], ["c", 3]]] Is that more what you were looking to do? Do you want the code? -- Jim Freeze