# Array .. more funkyness .. ;-)

**URL:** <https://www.ruby-forum.com/t/array-more-funkyness/85916>\
**Category:** Ruby\
**Created:** [February 17, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/array-more-funkyness/85916 "2007-02-17T16:50:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 17, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/array-more-funkyness/85916/1 "2007-02-17T16:50:43Z")

</div>

I need to produce such array :

root = [“22”, “friend 22”, [“11”, “contact 11”, [] ] , [“13”,  
“contact 13”], [“17”, “contact 17”]]  
writing

root[0] = friend.id\_to\_s  
root[1] = friend.pseudo

then for each friend.contact , I write an array  
friend\_array[0] = contact.id.to\_s  
friend\_array[1] = contact.pseudo  
… then I would like to write  
friend\_array[2] = [] if contact.info.count \> 0  
… I need to write an empty array in this case… (otherwise there is  
no friend\_array[2] )

how can I write it ?

tfyl

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 17, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/array-more-funkyness/85916/2 "2007-02-17T17:09:29Z")

</div>

Josselin wrote:

> I need to produce such array :
> 
> root = [“22”, “friend 22”, [“11”, “contact 11”, [] ] , [“13”,  
> “contact 13”], [“17”, “contact 17”]]  
> writing
> 
> root[0] = friend.id\_to\_s  
> root[1] = friend.pseudo
> 
> then for each friend.contact , I write an array  
> friend\_array[0] = contact.id.to\_s  
> friend\_array[1] = contact.pseudo  
> … then I would like to write  
> friend\_array[2] = [ï¿½] if contact.info.count \> 0  
> … I need to write an empty array in this case… (otherwise there is  
> no friend\_array[2] )
> 
> how can I write it ?
> 
> tfyl
> 
> joss

Either recursively or iteratively with a stack.  
def append\_friends(append\_to, friends)  
friends.each { |contact|  
as\_array = [contact.id.to\_s, contact.pseudo, []]  
append\_friends(as\_array[2], contact.friends)  
append\_to[2] \<\< as\_array  
}  
append\_to  
end

There are probably nicer ways to write the recursive variant. Feel free  
to evolve it.

Iterative version is left as an exercise to the reader 😉 (with ruby  
the iterative variant of this  
is most probably quite a bit faster btw.)

My regards

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 17, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/array-more-funkyness/85916/3 "2007-02-17T17:20:47Z")

</div>

On 2007-02-17 17:09:32 +0100, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > then for each friend.contact , I write an array
> 
> end
> 
> There are probably nicer ways to write the recursive variant. Feel free  
> to evolve it.
> 
> Iterative version is left as an exercise to the reader 😉 (with ruby  
> the iterative variant of this  
> is most probably quite a bit faster btw.)
> 
> My regards

thanks a lot ! that’s great… I’ll try to find a recursive variant  
later…

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 17, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/array-more-funkyness/85916/4 "2007-02-17T20:11:09Z")

</div>

Josselin wrote:

> On 2007-02-17 17:09:32 +0100, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:
> 
> > > then for each friend.contact , I write an array
> > 
> > end
> > 
> > There are probably nicer ways to write the recursive variant. Feel free  
> > to evolve it.
> > 
> > Iterative version is left as an exercise to the reader 😉 (with ruby  
> > the iterative variant of this  
> > is most probably quite a bit faster btw.)
> > 
> > My regards
> 
> thanks a lot ! that’s great… I’ll try to find a recursive variant  
> later…
> 
> joss

Seems to be a misunderstanding. The one I provided code for _is_ the  
recursive variant. In this case I found it easier to write and think it  
is easier to understand than the iterative version.

My regards

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 18, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/array-more-funkyness/85916/5 "2007-02-18T12:40:19Z")

</div>

On 2007-02-17 20:11:12 +0100, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > > Iterative version is left as an exercise to the reader 😉 (with ruby  
> > > Seems to be a misunderstanding. The one I provided code for _is_ the  
> > > recursive variant. In this case I found it easier to write and think it  
> > > is easier to understand than the iterative version.
> 
> My regards

got it… it runs very well
