Hi Abinoam,

You are true, I'm trying doing like C and hierarchical tree, what
better way to do this in ruby? It may be that my data structure is
bad, do you have some suggestion?

The ideia is create a menu hierarchical, where there self nestedly by paren=
t_id.
Thank you for help.

On Mon, Jan 17, 2011 at 02:23, Abinoam Jr. <abinoam / gmail.com> wrote:
> Hi Marcello,
>
> Sorry, but I was not able to figure out exactly how this piece of code
> is trying to generate your desired outcome. (my fault!)
> If you're just willing to represent the data in a kind o hierarchical
> tree, there's a lot of "Ruby way" to do that.
> It seams (in my humble opinion) that you're trying to do it in "C"
> way, or something like that.
> But, I can give you some tips. See comments in the code.
>
> # You shouldn't need to define methods outside the object scope (like
> the one bellow).
>
> # M=E9todos busca em profundidade iterativo
> def menu_iterative(menu)
> =A0res =3D []
>
> =A0# When shifting the first element you're assuming the Array is in a
> kind of order,
> =A0# and the first element is the "root". Is this always true?
> =A0el1 =3D menu.shift
> =A0res << el1
> =A0while not menu.empty?
> =A0 =A0res_aux =3D []
> =A0 =A0for i in menu
> =A0 =A0 =A0if el1.id =3D=3D i.parent_id
> =A0 =A0 =A0 =A0print el1," =3D=3D ",i,"* ",res_aux.index(el1),"\n"
> =A0 =A0 =A0 =A0res_aux << =A0i
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# The same shift/delete problem here
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# This elemente will never be reevaluated =
after deleted. And...
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Using delete here you're assuming that t=
he elements are unique.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Is this always true. If there's 2 "i" el=
ements in the Array, with
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# this line you'll be deleting the two of =
them.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# You can delete by the index instead.
> =A0 =A0 =A0 =A0el1 =3D menu.delete(i)
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# This use of retry should be considered d=
eprecated.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# It seems removed from Ruby 1.9.
> =A0 =A0 =A0 =A0# =A0 =A0 =A0 retry
> =A0 =A0 =A0 =A0 =A0else
> =A0 =A0 =A0 =A0print el1," !=3D ",i," \n"
> =A0 =A0 =A0end
> =A0 =A0end
> =A0 =A0res << res_aux unless res_aux.empty?
> =A0 =A0el1 =3D menu.shift
> =A0 =A0res << el1
> =A0end
> =A0# You don't need return here, the "return value" is always the last
> evaluated expression
> =A0return res
> end
>
> Good luck!
>
> On Fri, Jan 14, 2011 at 11:10 PM, Marcello Henrique <faraohh / gmail.com> w=
rote:
>> Hi Abinoam,
>>
>> Thanks for reply, actually the first number is 'id' and second
>> 'parent_id', so correct is:
>> ["1=3D>0",
>> =A0["2=3D>1",
>> =A0 =A0["3=3D>2",
>> =A0 =A0 =A0["7=3D>3"]
>> =A0 =A0],"5=3D>1","6=3D>1"
>> =A0],"4=3D>0"
>> ]
>>
>> From source flat:
>> [ "1=3D>0","2=3D>1", "3=3D>2","4=3D>0","5=3D>1","6=3D>1","7=3D>3"]
>>
>> The purpose is to use such as menu like this, based in example above:
>> <menu>
>> =A0<ul>
>> =A0 =A0<li class=3D"sub">"1=3D>0"
>> =A0 =A0 =A0<ul>
>> =A0 =A0 =A0 =A0<li class=3D"sub">"2=3D>1"
>> =A0 =A0 =A0 =A0 =A0<ul>
>> =A0 =A0 =A0 =A0 =A0 =A0<li class=3D"sub">"3=3D>2"</li>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0<ul>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<li>"7=3D>3"</li>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0</ul>
>> =A0 =A0 =A0 =A0 =A0 =A0</li>
>> =A0 =A0 =A0 =A0 =A0</ul>
>> =A0 =A0 =A0 =A0</li>
>> =A0 =A0 =A0 =A0<li>"5=3D>1"</li>
>> =A0 =A0 =A0 =A0<li>"6=3D>1"</li>
>> =A0 =A0 =A0</ul>
>> =A0 =A0</li>
>> =A0 =A0<li>"4=3D>0"</li>
>> =A0</ul>
>> </menu>
>>
>> Thanks in advance!
>>
>> On Fri, Jan 14, 2011 at 21:29, Abinoam Jr. <abinoam / gmail.com> wrote:
>>> Hi Marcello,
>>>
>>> For helping me understand your desired outcome I manually turned it
>>> into something more readable (just to read the nesting [] ).
>>>
>>> ["1=3D>0",["2=3D>1",["3=3D>2",["7=3D>3"]],["3=3D>2"]],["2=3D>1","5=3D>1=
","6=3D>1"]]
>>>
>>> arr[0] =3D "1=3D>0"
>>> arr[1] =3D ["2=3D>1", ["3=3D>2", ["7=3D>3"]], ["3=3D>2"]]
>>> arr[2] =3D ["2=3D>1", "5=3D>1", "6=3D>1"]
>>>
>>> Is this what you want?
>>>
>>> On Fri, Jan 14, 2011 at 11:54 AM, Marcello Henrique <faraohh / gmail.com>=
 wrote:
>>>> Hello,
>>>>
>>>> Short question:
>>>>
>>>> How to make an iterative method of stack?
>>>>
>>>> Long explanation:
>>>> I'm trying to create an iterative method for following structure:
>>>>
>>>> class Obj
>>>> =A0 attr_accessor: id,: parent_id
>>>>
>>>> =A0 def initialize (id, parent_id)
>>>> =A0 =A0 parent_id =3D @ parent_id
>>>> =A0 =A0 @ id =3D id
>>>> =A0 end
>>>>
>>>> =A0 def to_s
>>>> =A0 =A0 "id: # {@ id}, parent_id: @ # {parent_id} '
>>>> =A0 end
>>>> end
>>>>
>>>> left =3D [Obj.new (1.0), Obj.new (2.1), Obj.new (3.2), Obj.new (4.0),
>>>> Obj.new (5.1), Obj new (6.1), Obj.new (7.3)]
>>>>
>>>> My challenge is to make an iterative method, see how far I got:
>>>>
>>>> def menu_iterative (menu)
>>>> =A0 res =3D []
>>>>
>>>> =A0 el1 =3D menu.shift
>>>> =A0 res << el1
>>>> =A0 while not menu.empty?
>>>> =A0 =A0 res_aux =3D []
>>>> =A0 =A0 for i in menu
>>>> =A0 =A0 =A0 if el1.id =3D=3D i.parent_id
>>>> =A0 =A0 =A0 =A0 print el1, "=3D=3D" i "* ", res_aux.index (el1), "\ n"
>>>> =A0 =A0 =A0 =A0 res_aux << i
>>>> =A0 =A0 =A0 =A0 el1 =3D menu.delete (i)
>>>> =A0 =A0 =A0 =A0 retry
>>>> =A0 =A0 =A0 else
>>>> =A0 =A0 =A0 =A0 print el1, "! =3D" i, "\ n"
>>>> =A0 =A0 =A0 end
>>>> =A0 =A0 end
>>>> =A0 =A0 res << res_aux unless res_aux.empty?
>>>> =A0 =A0 puts
>>>> =A0 =A0 el1 =3D menu.shift
>>>> =A0 =A0 res << el1
>>>> =A0 end
>>>> =A0 return res
>>>> end
>>>>
>>>> puts "result:"
>>>> pp menu_iterative (left_it)
>>>>
>>>> I wish the outcome was:
>>>>
>>>> [*,
>>>> =A0[#<Obj:0x7f35b48cab18 @id=3D2, @parent_id=3D1>,
>>>> =A0[#<Obj:0x7f35b48caaf0 @id=3D3, @parent_id=3D2>,
>>>> =A0 [#<Obj:0x7f35b48caa50 @id=3D7, @parent_id=3D3>]],
>>>> =A0[#<Obj:0x7f35b48caaf0 @id=3D3, @parent_id=3D2>]],
>>>> =A0[#<Obj:0x7f35b48cab18 @id=3D2, @parent_id=3D1>,
>>>> =A0#<Obj:0x7f35b48caaa0 @id=3D5, @parent_id=3D1>,
>>>> =A0#<Obj:0x7f35b48caa78 @id=3D6, @parent_id=3D1>]]
>>>>
>>>> It seems that the problem is attribution without stack, correct?
>>>> I'm attaching all I did.
>>>>
>>>> Thanks for any help.
>>>> --
>>>> Marcello Henrique
>>>> Blog - http://faraohh.wordpress.com
>>>> Associa=E7=E3o Software Livre de Goi=E1s (www.aslgo.org.br)
>>>> Cercomp - UFG (www.cercomp.ufg.br)
>>>>
>>>
>>>
>>
>>
>>
>> --
>> Marcello Henrique
>> Blog - http://faraohh.wordpress.com
>> Associa=E7=E3o Software Livre de Goi=E1s (www.aslgo.org.br)
>> Cercomp - UFG (www.cercomp.ufg.br)
>>
>>
>
>



--=20
Marcello Henrique
Blog - http://faraohh.wordpress.com
Associa=E7=E3o Software Livre de Goi=E1s (www.aslgo.org.br)
Cercomp - UFG (www.cercomp.ufg.br)