From: Dave Burt Date: 2005-08-08T08:01:08+09:00 Subject: Re: cartesian product walter a kehowski... > c=[] > each {|ae| b.each {|be| c << [ae, be] } } > c.collect! {|ce| ce.flatten} Flatten is going to squash nested arrays. Maybe: inject([]) {|c, ae| c.concat b.map {|be| [ae, be] } } > > c=a[0] > as=a.slice(1..a.length-1) You can do this (in Perl, too): as = a.slice(1..-1) or this: as = a[1..-1] > as.each {|ase| c=c.cartprod(ase) } > c.each {|ce| ce.each {|x| print x," " }; print "\n" } > > Would the following be desirable? If cartprod has two or more arguments, > do the usual thing, but if it has just one, it takes the cartesian product > with that argument, i.e, cartprod(a) gives cartprod(a,a) or a.cartprod(a), > etc. I wouldn't. Typing an extra argument makes the function clearer - cartesian product is a two-array function. > And thanks for all the suggestion so far. I usually just play around with > Perl but got $#@% fatigue. It was a toss-up between Python and Ruby so > here I am. You're welcome. Cheers, Dave