Removing a single level of array nesting.

Discussion in 'Ruby' started by Kenosis, Jun 12, 2006.

  1. Kenosis

    Kenosis Guest

    Greetings Rubyologists,

    Suppose we have a simple array of arrays, e.g., [[1], [2], [3]]. What
    would be the cleanest way, if it's possible, to remove the outter level
    of array nesting such that the result is (a) a list of arrays: [1],
    [2], [3], and (b) suitable for passing as a list of arrays to
    GetoptLong. Thanks in advance for any help you can lend.

    Ken
    Kenosis, Jun 12, 2006
    #1
    1. Advertising

  2. Kenosis wrote:
    > Greetings Rubyologists,
    >
    > Suppose we have a simple array of arrays, e.g., [[1], [2], [3]]. What
    > would be the cleanest way, if it's possible, to remove the outter level
    > of array nesting such that the result is (a) a list of arrays: [1],
    > [2], [3], and (b) suitable for passing as a list of arrays to
    > GetoptLong. Thanks in advance for any help you can lend.


    Your array *is* already a "list" of arrays. Maybe you just need the
    star operator. Consider

    irb(main):001:0> def foo(*args) p args end
    => nil
    irb(main):002:0> foo([[1],[2]])
    [[[1], [2]]]
    => nil
    irb(main):003:0> foo(*[[1],[2]])
    [[1], [2]]
    => nil

    Kind regards

    robert
    Robert Klemme, Jun 12, 2006
    #2
    1. Advertising

  3. Kenosis

    Kenosis Guest

    Thanks Robert - that's what I thought too but my test in irb failed.
    Must be that I have to use *array when passing it to GetopLong.

    Regards,

    Ken

    Robert Klemme wrote:
    > Kenosis wrote:
    > > Greetings Rubyologists,
    > >
    > > Suppose we have a simple array of arrays, e.g., [[1], [2], [3]]. What
    > > would be the cleanest way, if it's possible, to remove the outter level
    > > of array nesting such that the result is (a) a list of arrays: [1],
    > > [2], [3], and (b) suitable for passing as a list of arrays to
    > > GetoptLong. Thanks in advance for any help you can lend.

    >
    > Your array *is* already a "list" of arrays. Maybe you just need the
    > star operator. Consider
    >
    > irb(main):001:0> def foo(*args) p args end
    > => nil
    > irb(main):002:0> foo([[1],[2]])
    > [[[1], [2]]]
    > => nil
    > irb(main):003:0> foo(*[[1],[2]])
    > [[1], [2]]
    > => nil
    >
    > Kind regards
    >
    > robert
    Kenosis, Jun 12, 2006
    #3
  4. Kenosis <> wrote:
    > Thanks Robert - that's what I thought too but my test in irb failed.
    > Must be that I have to use *array when passing it to GetopLong.


    That's what I was insinuating. :)

    robert
    Robert Klemme, Jun 12, 2006
    #4
    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. Bijoy Naick

    Three Level Repeater Nesting - HELP!!!

    Bijoy Naick, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,871
    Bijoy Naick
    Aug 10, 2004
  2. Bijoy Naick

    RE: Three Level Repeater Nesting - HELP!!!

    Bijoy Naick, Aug 10, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    367
    Bijoy Naick
    Aug 10, 2004
  3. pabbu
    Replies:
    8
    Views:
    719
    Marc Boyer
    Nov 7, 2005
  4. Replies:
    4
    Views:
    323
    Victor Bazarov
    Apr 10, 2006
  5. Trans
    Replies:
    10
    Views:
    294
    Sean O'Halpin
    Sep 16, 2005
Loading...

Share This Page