From: Steve Litt Date: 2005-12-09T11:27:33+09:00 Subject: Re: New guy... Intoduction and first question on some direct On Thursday 08 December 2005 06:39 pm, Oscar Gonzalez wrote: > Well thats a lot of info so I have to digest on it. I'll post back as > soon as I have a better grasp of your responses... However I do not want > to use Lua for this because I want to learn Ruby... I don't think its a > problem that the data is in Lua syntax, from what I can see, it doesnt > matter what format the data is in. It seems to be a matter of finding a > pattern and being able to merge the data from two files into one. > > For the purpose of being accurate on the smple, I've posted the file on > my site so maybe if you see the actual file I'm working with you'll get > a better idea of what I want. > > http://www.muychingon.com/gatherer.txt Below my sig is a 45 line program using Node.rb that converts the file into Node objects, each with a name and value. You can see how Walker objects and callback routines work. In order to output your chosen format (which I didn't completely understand), you'd need to create probably a couple more Walkers and a couple more callback routines. This program assumes consistent indentation. If that cannot be assumed, you need to either do something else (maybe what Bill Guindon suggested), or create a tiny brace to indent converter and then run the result through my program. HTH SteveT Steve Litt http://www.troubleshooters.com slitt@troubleshooters.com #!/usr/bin/ruby require "Node.rb" class Callbacks def cb_look_data(checker, level) print "\t" * level print "Name = ", checker.name print ", Value = " , checker.value unless checker.firstchild print "\n" end def cb_get_fields(checker, level) if checker.value =~ /\s*}/ checker.deleteSelf() end checker.value.gsub!(/,\s*$/, "") checker.value.strip! checker.value =~ /\[([^\]]*)\]/ checker.name = $1 if $1 if level == 1 checker.value =~ /(.*)\s*=/ checker.name = $1 if $1 end checker.value =~ /=\s*(.*)/ checker.value = $1 if $1 checker.value = "" if checker.value == "{" end end cb = Callbacks.new() # INSTANTIATE CALLBACKS OBJECT #### PARSE THE FILE parser = OutlineParser.new() head = parser.parse("/home/slitt/gatherer.txt") #### PARSE THE NODE TREE NODES INTO NAME AND VALUE FIELDS walker = Walker.new(head, cb.method(:cb_get_fields), nil) walker.walk() #### PRINT THE NAME FIELDS FOR CONTAINERS, #### AND NAME AND VALUE FIELDS FOR LEAF LEVELS walker = Walker.new(head, cb.method(:cb_look_data), nil) walker.walk()