From: Paul Lutus Date: 2006-11-28T04:00:13+09:00 Subject: Re: How to get data from html table Vikash Kumar wrote: > I want to store the values of a table in different variables, I have the > following table structure: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
 Case5-0410/11/2006 23:24:33Case5-041005Sell1,000ARP$36.90
 Case5-0310/11/2006 23:20:07Case5-031005Buy1,500ARP$36.70
 Case4-0410/11/2006 05:28:54Case4-041004Sell300RIL$490.00
 Case4-0310/11/2006 05:21:32Case4-031004Buy200RIL$489.90
> > I want to store the values in variables so that I can compare records. > Please help me out how to do this in ruby. Only a few lines of Ruby are required to accomplish this: ------------------------------------------ #!/usr/bin/ruby -w data = File.read(sourcefilename) output = [] html_rows = data.scan(%r{(.*?)}im).flatten html_rows.each do |row| # filter these undesired elements row.gsub!(" ","") row.gsub("","") cells = row.scan(%r{(.*?)}im).flatten output << cells end # done collecting, now display output.each do |row| line = row.join(",") puts line end ------------------------------------------ Output: Case5-04,10/11/2006 23:24:33,Case5-04,1005,Sell,1,000,ARP,$36.90 Case5-03,10/11/2006 23:20:07,Case5-03,1005,Buy,1,500,ARP,$36.70 Case4-04,10/11/2006 05:28:54,Case4-04,1004,Sell,300,RIL,$490.00 Case4-03,10/11/2006 05:21:32,Case4-03,1004,Buy,200,RIL,$489.90 BTW there are some errors in your HTML sample. One example are some orphan " half-tags. They are not difficult to filter out of the result. One reason this sort of example is difficult to parse is because of the errors. But this specific parser will do the basic parsing, and you can always filter a few errors, as this code does. Digression: when solving a problem like this, it is often much easier to write a few lines of HTML than to try to use a high-powered library to accomplish it. -- Paul Lutus http://www.arachnoid.com