# Can I use Hpricot to parse data into different array elem?

**URL:** <https://www.ruby-forum.com/t/can-i-use-hpricot-to-parse-data-into-different-array-elem/168527>\
**Category:** Ruby\
**Created:** [May 21, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/can-i-use-hpricot-to-parse-data-into-different-array-elem/168527 "2009-05-21T22:59:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clarinet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@clarinet](https://www.ruby-forum.com/u/clarinet)\
**Post date:** [May 21, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/can-i-use-hpricot-to-parse-data-into-different-array-elem/168527/1 "2009-05-21T22:59:33Z")

</div>

Hi,

I have a question about using Hpricot.

Say I have a file search.html that contains the following:

- 

```
        <a

```

href=“http://www.sheetmusicplus.com/title/Holst-G-Wind-Quintet-Set/5015093”\>Holst  
G /Wind Quintet (Set)

```
      <span class="price">$21.95</span>

```

- 

```
        <a

```

href=“http://www.sheetmusicplus.com/title/Holst-G-Wind-Quintet-Score/5014486”\>Holst  
G /Wind Quintet(Score)

```
      <span class="price">$15.95</span>

```

And I want to use Hpricot to parse it and have each price matched with  
its title.

If I do something like this

doc = open(“F:/search.html”) { |f| Hpricot(f) }  
doc.search(".price").each do |price|  
prices = [price.inner\_text]  
puts prices[0]

=\>$21.95  
$15.95

Then it seems that my search results are in an array, but that both  
prices are in a single element in the array because both come up when I  
ask for the element at [0]  
I’m a ruby newbie–what am I missing?  
Thanks very much

---

<div class="post-metadata">

**Author:** ![clarinet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@clarinet](https://www.ruby-forum.com/u/clarinet)\
**Post date:** [May 22, 2009, 7:11am UTC](https://www.ruby-forum.com/t/can-i-use-hpricot-to-parse-data-into-different-array-elem/168527/2 "2009-05-22T07:11:23Z")

</div>

Christiaan Venter wrote:

> Hi,
> 
> I have a question about using Hpricot.
> 
> If I do something like this
> 
> doc = open(“F:/search.html”) { |f| Hpricot(f) }  
> doc.search(".price").each do |price|  
> prices = [price.inner\_text]  
> puts prices[0]
> 
> =\>$21.95  
> $15.95
> 
> Then it seems that my search results are in an array, but that both  
> prices are in a single element in the array because both come up when I  
> ask for the element at [0]  
> I’m a ruby newbie–what am I missing?  
> Thanks very much

Not understanding loops? Better variable names?

require ‘rubygems’  
require ‘hpricot’

doc = open(“html.txt”) { |f| Hpricot(f) }

doc.search(".price").each do |price\_tag|  
price = price\_tag.inner\_text  
puts price  
end

–output:–  
$21.95  
$15.95
