[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

array .. more funkyness .. ;-

Josselin

2/17/2007 3:46:00 PM

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



4 Answers

Stefan Rusterholz

2/17/2007 4:10:00 PM

0

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


--
Posted via http://www.ruby-....

Josselin

2/17/2007 4:16:00 PM

0

On 2007-02-17 17:09:32 +0100, Stefan Rusterholz <apeiros@gmx.net> said:

> 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

thanks a lot ! that's great... I'll try to find a recursive variant later..

joss

Stefan Rusterholz

2/17/2007 7:11:00 PM

0

Josselin wrote:
> On 2007-02-17 17:09:32 +0100, Stefan Rusterholz <apeiros@gmx.net> 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

--
Posted via http://www.ruby-....

Josselin

2/18/2007 11:38:00 AM

0

On 2007-02-17 20:11:12 +0100, Stefan Rusterholz <apeiros@gmx.net> said:

> Josselin wrote:
>> On 2007-02-17 17:09:32 +0100, Stefan Rusterholz <apeiros@gmx.net> 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

got it.. it runs very well