From: Peter Szinek Date: 2007-03-05T21:11:42+09:00 Subject: Re: ruby noob mosfet wrote: > Hi, > > I would like to parse a very simple html(index_msg.htm) file described > below : > > > WM_ACTIVATE > 0x0006 > > 0x0000 > WM_NULL > > > WM_ACTIVATEAPP > 0x001C > > 0x0001 > WM_CREATE > > ... > I would like to parse this file and to extract information like this : > > enum foo > { > eWM_ACTIVATE = 0x0006, > eWM_ACTIVATEAPP = 0x0001, > ... > }; > > I am starting with this : > > > fileIn = File.open("C:/WIKI_CE/index_msg.htm", "r") > fileOut = File.new("C:/WIKI_CE/enumWmMsg.h", "w") > > begin > while (line = fileIn.readline) > line.chomp > $stdout.print line > end > rescue EOFError > fileIn.close > fileOut.close > end This should get you started: ===================================================================== require 'rubygems' require 'scrubyt' data = Scrubyt::Extractor.define do fetch('input.html') record do var_name 'WM_ACTIVATE' code '0x0006' end end result = data.to_xml.to_s names = result.scan(/var_name>(.+?)<\/var_name/).flatten values = result.scan(/code>(.+?)<\/code/).flatten pairs = names.zip(values) pairs.each do |name, value| puts "e#{name} = #{value}" end ===================================================================== The XML to array code kind of sucks, in the next version of scRUBYt! you will be able to output the result directly to a hash (or CSV or YAML or some other, more friendly format for such a task). Cheers, Peter __ http://www.rubyrailways.com :: Ruby and Web2.0 blog http://scrubyt.org :: Ruby web scraping framework http://rubykitchensink.ca/ :: The indexed archive of all things Ruby