On Thu, 28 Jul 2005, Claus Spitzer wrote:

> Greetings!
>
> Let me start by clarifying that this is just a request for style
> suggestions... So I won't be hugely disappointed if the plain old way
> works just fine.
>
> 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 ] ]

i felt obiliged to send this after messing it up before ;-)

   harp:~ > cat a.rb
   foo = [ "a", "b", "c", 'n' ]
   bar = [  1 ,  2 ,  3 , 'm' ]

   cross = foo.inject(a=[]){|a,f| bar.inject(a){|a,b| a << [f,b]}}
   p cross

   harp:~ > ruby a.rb
   [["a", 1], ["a", 2], ["a", 3], ["a", "m"], ["b", 1], ["b", 2], ["b", 3], ["b", "m"], ["c", 1], ["c", 2], ["c", 3], ["c", "m"], ["n", 1], ["n", 2], ["n", 3], ["n", "m"]]

i know it's just two loops  - but at least it's one line ;-)

kind regards.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple.  My religion is kindness.
| --Tenzin Gyatso
===============================================================================