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® }
puts prod.to_s
end

There is the input

ranges = [
[[‘a’, ‘b’], [‘c’, ‘d’]],
[[‘e’, ‘f’], [‘g’, ‘h’]],
[[‘i’, ‘j’], [‘k’, ‘l’]]
]
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

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

2011/12/8 Sbastien M. [email protected]:

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…

On Thu, Dec 8, 2011 at 10:04 AM, Sbastien M. [email protected] wrote:

[[“a”, “b”], [“e”, “f”], [“k”, “l”]],

]

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

ok resolved it

forgot the use of the star operator…

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

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® }
puts prod.to_s
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs