From: daz Date: 2005-10-13T13:46:53+09:00 Subject: Re: array1 + array2 newb question Mike Schramm wrote: > Hey all, > > I'm a ruby newbie, so this will be obvious, but I can't figure it out. > > I'm trying to create a map grid from arrayed names of streets. I > want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a], > [1,b],... [3,c]]. > > so far the closest I have gotten is > > a1.collect!{|x| [x, a2.collect!{|y| y}]} > The outer iteration should be on a2 ... #---------------------------------------------------- ANSWER = [[1, :a], [2, :a], [3, :a], [1, :b], [2, :b], [3, :b], [1, :c], [2, :c], [3, :c]] a1 = [1, 2, 3] a2 = [:a, :b, :c] r = []; a2.each { |e2| r += a1.map {|e1| [e1, e2] } } p r, ANSWER p r == ANSWER #---------------------------------------------------- daz (Thought I'd better bail out some of the duff answerers ;p)