From: Marcello Henrique Date: 2011-01-15T11:10:50+09:00 Subject: Re: Ruby iterative depth. Hi Abinoam, Thanks for reply, actually the first number is 'id' and second 'parent_id', so correct is: ["1=>0", ["2=>1", ["3=>2", ["7=>3"] ],"5=>1","6=>1" ],"4=>0" ] From source flat: [ "1=>0","2=>1", "3=>2","4=>0","5=>1","6=>1","7=>3"] The purpose is to use such as menu like this, based in example above: Thanks in advance! On Fri, Jan 14, 2011 at 21:29, Abinoam Jr. wrote: > Hi Marcello, > > For helping me understand your desired outcome I manually turned it > into something more readable (just to read the nesting [] ). > > ["1=>0",["2=>1",["3=>2",["7=>3"]],["3=>2"]],["2=>1","5=>1","6=>1"]] > > arr[0] = "1=>0" > arr[1] = ["2=>1", ["3=>2", ["7=>3"]], ["3=>2"]] > arr[2] = ["2=>1", "5=>1", "6=>1"] > > Is this what you want? > > On Fri, Jan 14, 2011 at 11:54 AM, Marcello Henrique 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 >>   attr_accessor: id,: parent_id >> >>   def initialize (id, parent_id) >>     parent_id = @ parent_id >>     @ id = id >>   end >> >>   def to_s >>     "id: # {@ id}, parent_id: @ # {parent_id} ' >>   end >> end >> >> left = [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) >>   res = [] >> >>   el1 = menu.shift >>   res << el1 >>   while not menu.empty? >>     res_aux = [] >>     for i in menu >>       if el1.id == i.parent_id >>         print el1, "==" i "* ", res_aux.index (el1), "\ n" >>         res_aux << i >>         el1 = menu.delete (i) >>         retry >>       else >>         print el1, "! =" i, "\ n" >>       end >>     end >>     res << res_aux unless res_aux.empty? >>     puts >>     el1 = menu.shift >>     res << el1 >>   end >>   return res >> end >> >> puts "result:" >> pp menu_iterative (left_it) >> >> I wish the outcome was: >> >> [*, >>  [#, >>  [#, >>   [#]], >>  [#]], >>  [#, >>  #, >>  #]] >> >> 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ção Software Livre de Goiás (www.aslgo.org.br) >> Cercomp - UFG (www.cercomp.ufg.br) >> > > -- Marcello Henrique Blog - http://faraohh.wordpress.com Associação Software Livre de Goiás (www.aslgo.org.br) Cercomp - UFG (www.cercomp.ufg.br)