How to read a value from a table in tuby

How to read and write a table in ruby

···

--
Posted via http://www.ruby-forum.com/.

What sort of table? HTML, Database, CSV, Excel?

Assuming you mean HTML, here's a little snippet of script that uses
Hpricot to parse the HTML and convert it to a Ruby Array of Arrays.

  html = IO.read( 'file/on/disk.html' )
  doc = Hpricot( html )
  table = (doc/"table#buglist//tr").map{ |row|
    (row/"td[@colspan='']").map{ |cell|
    cell.inner_text.strip
    }
  }.compact

The above finds a table with id="buglist" in the html file, and finds
every cell that does not have a colspan attribute. See Hpricot, XPath,
or other documentation for more information on finding out how to find
the data you want.

To write an HTML table, you can use an existing library, or write out
the text yourself.

More details are needed to help you more.

···

On Sep 12, 12:02 pm, Rupa Ruby <anu.re...@gmail.com> wrote:

How to read and write a table in ruby

Hi,
  Thanks for your answer. I mentioned HTML table.As Iam learning Ruby
and it is new to me, I have 3 basic questions. I would appreciate if you
can answer the questions below:

1 How to read from a HTML table row by row in Ruby?

2 How to write to a HTML table row by row in Ruby?
                              For Instance, In buy wizard there is table
containing rows and columns. I want to pass values for each row. If you
look at my script, I have passed different values for each row.But, if I
pass values for each row, ruby is not accepting and showing an error. Is
there any way to write values in table?

3 How to write a script that is independent of data ? Would like the
script to read the data from data file instead of hard coding it in the
script itself ?
                           For Instance, I am entering a particular
details in a first row. Tomorrow if I need to change the row details,I
need to go and change the script. Can i make the program so generic to
read the data from an external file.

I would appreciate your help on tbis.
Thanks,
Anu

Gavin Kistner wrote:

···

On Sep 12, 12:02 pm, Rupa Ruby <anu.re...@gmail.com> wrote:

How to read and write a table in ruby

What sort of table? HTML, Database, CSV, Excel?

Assuming you mean HTML, here's a little snippet of script that uses
Hpricot to parse the HTML and convert it to a Ruby Array of Arrays.

  html = IO.read( 'file/on/disk.html' )
  doc = Hpricot( html )
  table = (doc/"table#buglist//tr").map{ |row|
    (row/"td[@colspan='']").map{ |cell|
    cell.inner_text.strip
    }
  }.compact

The above finds a table with id="buglist" in the html file, and finds
every cell that does not have a colspan attribute. See Hpricot, XPath,
or other documentation for more information on finding out how to find
the data you want.

To write an HTML table, you can use an existing library, or write out
the text yourself.

More details are needed to help you more.

--
Posted via http://www.ruby-forum.com/\.