# How to read a value from a table in tuby

**URL:** <https://www.ruby-forum.com/t/how-to-read-a-value-from-a-table-in-tuby/114448>\
**Category:** Ruby\
**Created:** [September 12, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-read-a-value-from-a-table-in-tuby/114448 "2007-09-12T20:02:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 12, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-read-a-value-from-a-table-in-tuby/114448/1 "2007-09-12T20:02:13Z")

</div>

How to read and write a table in ruby

---

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 12, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/how-to-read-a-value-from-a-table-in-tuby/114448/2 "2007-09-12T21:04:05Z")

</div>

On Sep 12, 12:02 pm, Rupa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 13, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/how-to-read-a-value-from-a-table-in-tuby/114448/3 "2007-09-13T20:54:59Z")

</div>

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 K. wrote:

> On Sep 12, 12:02 pm, Rupa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
