Removing a multiple array layer

Hi,

I'm trying to remove a array layer implied by multiple Array#product

Here is the code

···

----------------
def calculate ranges
  prod = ranges.shift
  ranges.each { |r| prod = prod.product(r) }
  puts prod.to_s
end
---------------------------------------------
Here is what i do get
----------------------
[
  [[["a", "b"], ["e", "f"]], ["i", "j"]],
  [[["a", "b"], ["e", "f"]], ["k", "l"]],
  ...
]
-------------------------------------------
And there is what i wish
------------------------
[
  [["a", "b"], ["e", "f"], ["i", "j"]],
  [["a", "b"], ["e", "f"], ["k", "l"]],
  ...
]

Probably a silly question, but it's driving me mad.
I tried flatten, prod = prod[0], using Array#permutation, ...
Can't find my way out..???

Thanks

--
Posted via http://www.ruby-forum.com/.

Try prod = prod.product(ranges) without loop.

···

2011/12/8 Sébastien M. <stkfdt@gmail.com>:

Hi,

I'm trying to remove a array layer implied by multiple Array#product

Here is the code
----------------
def calculate ranges
prod = ranges.shift
ranges.each { |r| prod = prod.product(r) }
puts prod.to_s
end
---------------------------------------------
Here is what i do get
----------------------
[
[[["a", "b"], ["e", "f"]], ["i", "j"]],
[[["a", "b"], ["e", "f"]], ["k", "l"]],
...
]
-------------------------------------------
And there is what i wish
------------------------
[
[["a", "b"], ["e", "f"], ["i", "j"]],
[["a", "b"], ["e", "f"], ["k", "l"]],
...
]

Probably a silly question, but it's driving me mad.
I tried flatten, prod = prod[0], using Array#permutation, ...
Can't find my way out..???

Thanks

--
Posted via http://www.ruby-forum.com/\.

ok resolved it

forgot the use of the star operator....

def calculate ranges
  prod = ranges.shift.product(*ranges)
  puts prod.to_s
end

···

--
Posted via http://www.ruby-forum.com/.

Tryed but does not the opposite expected output
[
  [["a", "b"], [["e", "f"], ["i", "j"]]],
  [["a", "b"], [["e", "f"], ["k", "l"]]],
  ...
]

what i try to get is
[
  [["a", "b"], ["e", "f"], ["i", "j"]],
  [["a", "b"], ["e", "f"], ["k", "l"]],
  ...
]

and i tries for the past 3 hours... getting tired of trying.

If anyone knows...

···

--
Posted via http://www.ruby-forum.com/.

Are you claiming your input arrays are as follows?

x = [['a', 'b']]
y = [[['e', 'f'], ['i', 'j']], [['e', 'f'], ['k', 'l']]]

That seems to be the only way to get what you are getting back with
x.product(y)

If that's the case then you would appear to want

y.map{ |z| x + z }

That would return your desired array. At least from the limited information
you've provided.

John

···

On Thu, Dec 8, 2011 at 10:04 AM, Sébastien M. <stkfdt@gmail.com> wrote:

Tryed but does not the opposite expected output
[
  [["a", "b"], [["e", "f"], ["i", "j"]]],
[["a", "b"], [["e", "f"], ["k", "l"]]],
...
]

what i try to get is
[
  [["a", "b"], ["e", "f"], ["i", "j"]],
[["a", "b"], ["e", "f"], ["k", "l"]],
...
]

Sorry

Xas away for a fex days;

my input array is
ranges = [
  [['a', 'b'], ['c', 'd']],
  [['e', 'f'], ['g', 'h']],
  [['i', 'j'], ['k', 'l']]
]

and i wish to get
[
  [["a", "b"], ["e", "f"], ["i", "j"]],
  [["a", "b"], ["e", "f"], ["k", "l"]],
  [["a", "b"], ["g", "h"], ["i", "j"]],
  [["a", "b"], ["g", "h"], ["k", "l"]],
  [["c", "d"], ["e", "f"], ["i", "j"]],
  [["c", "d"], ["e", "f"], ["k", "l"]],
  [["c", "d"], ["g", "h"], ["i", "j"]],
  [["c", "d"], ["g", "h"], ["k", "l"]],
]

wich is actually the product, all combination possibles of the various
'ranges'

but when i run it through a Array#product i get an added array level
whish i would like to remove.

reminder here is the method
def calculate ranges
  prod = ranges.shift
  ranges.each { |r| prod = prod.product(r) }
  puts prod.to_s
end

···

--
Posted via http://www.ruby-forum.com/.