multi-dimensional arrays to 2-dimensional arrays

Discussion in 'Ruby' started by Wirianto Djunaidi, Apr 25, 2008.

  1. [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] ]

    Thanks in advance,
    -DJ
    Wirianto Djunaidi, Apr 25, 2008
    #1
    1. Advertising

  2. 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] ]
    >
    > Thanks in advance,
    > -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.
    M. Edward (Ed) Borasky, Apr 26, 2008
    #2
    1. Advertising

  3. [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
    Wirianto Djunaidi, Apr 29, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,659
    Jim Lewis
    Mar 21, 2006
  2. Kevin
    Replies:
    10
    Views:
    1,231
    Chris Uppal
    Sep 15, 2005
  3. Alf P. Steinbach
    Replies:
    0
    Views:
    435
    Alf P. Steinbach
    Aug 18, 2003
  4. John Harrison
    Replies:
    4
    Views:
    6,924
    Default User
    Aug 19, 2003
  5. Icosahedron
    Replies:
    8
    Views:
    653
    Vivek
    Aug 21, 2003
Loading...

Share This Page