Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   multi-dimensional arrays to 2-dimensional arrays (http://www.velocityreviews.com/forums/t849922-multi-dimensional-arrays-to-2-dimensional-arrays.html)

 Wirianto Djunaidi 04-25-2008 04:39 PM

multi-dimensional arrays to 2-dimensional arrays

[Note: parts of this message were removed to make it a legal post.]

Hi,

I have a recursive method that aggregates data in the form of 2-dimensional
arrays.
The problem is at the top of the recursion call, I ended up with
multi-dimensional arrays which depend
on how deep the recursion is.

What is the best way to flatten multi-dimensional arrays back into
2-dimensional arrays?
the data might look like this:
[ [a, b, c], [ [ [d, e], [f, g] ], [h, i] ]
and I would like to be flatten into:
[ [a, b, c], [d, e], [f, g], [h, i] ]

-DJ

 M. Edward (Ed) Borasky 04-26-2008 03:53 AM

Re: multi-dimensional arrays to 2-dimensional arrays

Wirianto Djunaidi wrote:
> Hi,
>
> I have a recursive method that aggregates data in the form of 2-dimensional
> arrays.
> The problem is at the top of the recursion call, I ended up with
> multi-dimensional arrays which depend
> on how deep the recursion is.
>
> What is the best way to flatten multi-dimensional arrays back into
> 2-dimensional arrays?
> the data might look like this:
> [ [a, b, c], [ [ [d, e], [f, g] ], [h, i] ]
> and I would like to be flatten into:
> [ [a, b, c], [d, e], [f, g], [h, i] ]
>
> -DJ
>

I don't think you've properly defined your inputs and outputs. You
shouldn't need an extra "flattening" step if your recursion is correctly
defined.

 Wirianto Djunaidi 04-29-2008 07:31 AM

Re: multi-dimensional arrays to 2-dimensional arrays

[Note: parts of this message were removed to make it a legal post.]

Yeah, I figure out my problem. The reason I got nested multi-dimension array
is each recursion return a 2-d array. And when I combined them together I
use <<, which add the whole 2-d as 1 object into the result at the lower
stack. I switch it to use += to combine the array and that fix it. :P

Thanks all,
-DJ

 All times are GMT. The time now is 06:13 PM.