[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Removing a single level of array nesting.

Kenosis

6/12/2006 6:18:00 PM

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

3 Answers

Robert Klemme

6/12/2006 8:01:00 PM

0

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

6/12/2006 8:43:00 PM

0

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

Robert Klemme

6/12/2006 9:01:00 PM

0

Kenosis <kenosis@gmail.com> 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