# Storing values in variables

**URL:** <https://www.ruby-forum.com/t/storing-values-in-variables/80204>\
**Category:** Ruby\
**Created:** [December 22, 2006, 4:37am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204 "2006-12-22T04:37:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 4:37am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/1 "2006-12-22T04:37:14Z")

</div>

I am using the following code:

require ‘net/http’

# read the page data

http = Net::HTTP.new(‘[kvcrpf.org](http://kvcrpf.org)’, 80)  
resp, page = http.get(‘/achievements.htm’, nil )

# BEGIN processing HTML

def parse\_html(data,tag)  
return data.scan(%r{\<#{tag}\s\*._?\>(._?)\</#{tag}\>}im).flatten  
end

output = []  
table\_data = parse\_html(page,“table”)  
table\_data.each do |table|  
out\_row = []  
row\_data = parse\_html(table,“tr”)  
row\_data.each do |row|  
cell\_data = parse\_html(row,“td”)  
cell\_data.each do |cell|  
cell.gsub!(%r{\<.\*?\>},“”)  
end  
out\_row \<\< cell\_data  
end  
output \<\< out\_row  
end

# END processing HTML

# examine the result

def parse\_nested\_array(array,tab = 0)  
n = 0  
array.each do |item|  
if(item.size \> 0)  
puts “#{”\t" \* tab}[#{n}] {"  
if(item.class == Array)  
parse\_nested\_array(item,tab+1)  
else  
puts “#{”\t" \* (tab+1)}#{item}"  
end  
puts “#{”\t" \* tab}}"  
end  
n += 1  
end  
end

parse\_nested\_array(output[2][4])

It displays the output like this:

[0] {  
2004  
}  
[1] {  
65  
}  
[2] {  
58  
}  
[3] {  
89.23  
}

I want to store the value 2004, 65, 58, 89.23 in variables like this:

aa = 2004

ab = 65

ac = 58

ad = 89.23

Please help me, in doing this.

Thanks in advance

Vikash

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 5:40am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/2 "2006-12-22T05:40:35Z")

</div>

Vikash Kumar wrote:  
[snip]

> [2] {  
> ab = 65
> 
> ac = 58
> 
> ad = 89.23

Do you need them to be local variables, or can they be instance  
variables, or symbols in a hash?  
Do you want them to be sequentially labelled, like you have them here  
(e.g. ‘aa’ … ‘zz’ ) or are those just example names? Would you instead  
want to supply an array of strings that are the names for each element?

Does all the HTML processing have anything to do with this, or is it  
just that you have a nested array that you want to ‘splat’ to varaibles?

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 5:44am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/3 "2006-12-22T05:44:46Z")

</div>

Gavin K. wrote:

> Vikash Kumar wrote:  
> [snip]
> 
> > [2] {  
> > ab = 65
> > 
> > ac = 58
> > 
> > ad = 89.23
> 
> Do you need them to be local variables, or can they be instance  
> variables, or symbols in a hash?  
> Do you want them to be sequentially labelled, like you have them here  
> (e.g. ‘aa’ … ‘zz’ ) or are those just example names? Would you instead  
> want to supply an array of strings that are the names for each element?
> 
> Does all the HTML processing have anything to do with this, or is it  
> just that you have a nested array that you want to ‘splat’ to varaibles?

I want to just store these values in local variables, any help will be  
appreciated.

Thanks  
Vikash

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 6:49am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/4 "2006-12-22T06:49:58Z")

</div>

Vikash Kumar wrote:

> Gavin K. wrote:
> 
> > Vikash Kumar wrote:  
> > [snip]
> > 
> > > [2] {  
> > > ab = 65
> > > 
> > > ac = 58
> > > 
> > > ad = 89.23
> > 
> > Do you need them to be local variables, or can they be instance  
> > variables, or symbols in a hash?  
> > Do you want them to be sequentially labelled, like you have them here  
> > (e.g. ‘aa’ … ‘zz’ ) or are those just example names? Would you instead  
> > want to supply an array of strings that are the names for each element?

> I want to just store these values in local variables, any help will be  
> appreciated.
> 
> Thanks  
> Vikash

any local variables, a = 65, l = 58, m = 89.23

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 6:56am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/5 "2006-12-22T06:56:42Z")

</div>

Vikash Kumar wrote:

> I want to just store these values in local variables, any help will be  
> appreciated.

I would like to try to help, but I think I need more information.  
Please answer the rest of my questions.

For what it’s worth, dynamically creating local variables (with  
script-derived names) is slightly harder than dynamically creating  
instance variables or hash entries. The wording of your last answer  
makes me wonder if you thought that asking for local variables was the  
easiest way; would your solution be just as good if it used instance  
variables or hash entries instead of local variables?

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 7:01am UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/6 "2006-12-22T07:01:10Z")

</div>

Gavin K. wrote:

> Vikash Kumar wrote:
> 
> > I want to just store these values in local variables, any help will be  
> > appreciated.
> 
> I would like to try to help, but I think I need more information.  
> Please answer the rest of my questions.
> 
> For what it’s worth, dynamically creating local variables (with  
> script-derived names) is slightly harder than dynamically creating  
> instance variables or hash entries. The wording of your last answer  
> makes me wonder if you thought that asking for local variables was the  
> easiest way; would your solution be just as good if it used instance  
> variables or hash entries instead of local variables?

Storing those values in local variables will be fine, any local  
variables, a = 65, l = 58, m = 89.23

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/7 "2006-12-22T13:43:20Z")

</div>

Vikash Kumar wrote:  
[…]

> ```
> puts "#{"\t" * tab}}"
> 
> ```
> 
> 2004
> 
> I want to store the value 2004, 65, 58, 89.23 in variables like this:
> 
> aa = 2004  
> ab = 65  
> ac = 58  
> ad = 89.23

aa, ab, ac, ad = output[2][4]

Good luck.

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [December 22, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/8 "2006-12-22T18:04:00Z")

</div>

Vikash Kumar wrote:

> I am using the following code:  
> Vikash – since Carlos answered your question, I’m going to give you  
> some unsolicited tips.

> def parse\_html(data,tag)  
> return data.scan(%r{\<#{tag}\s\*._?\>(._?)\</#{tag}\>}im).flatten  
> end  
> Have you looked at hpricot? (Google it.) With it, your code’ll look  
> something like:  
> def parse\_html(el, tag)  
> el.search("//#{tag}")  
> end

> ```
> out_row << cell_data
> 
> ```
> 
> end  
> output \<\< out\_row  
> end  
> Get to know Array#map. Your code’ll look something like:  
> output = parse\_html(page, ‘table’).map do |table|  
> parse\_html(table, ‘tr’).map do |row|  
> parse\_html(row,‘td’).map do |cell|  
> cell.inner\_html.gsub(%r{\<.\*?\>},"")  
> end  
> end  
> end

> ```
> puts "#{"\t" * tab}}"
> end
> n += 1
> 
> ```
> 
> end  
> end

1. Array#each\_with\_index will keep track of n for you.
2. Array#inspect or Kernel#p will print the array in a readable format.

> parse\_nested\_array(output[2][4])  
> If you’re only interested in the first four values, then  
> a,b,c,d=output[2][4] will work. Otherwise, I think we need more  
> information.

Devin

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [January 15, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/storing-values-in-variables/80204/9 "2007-01-15T12:46:15Z")

</div>

> > def parse\_html(data,tag)  
> > return data.scan(%r{\<#{tag}\s\*._?\>(._?)\</#{tag}\>}im).flatten  
> > end  
> > Have you looked at hpricot? (Google it.) With it, your code’ll look  
> > something like:  
> > def parse\_html(el, tag)  
> > el.search(“//#{tag}”)  
> > end

> Get to know Array#map. Your code’ll look something like:  
> output = parse\_html(page, ‘table’).map do |table|  
> parse\_html(table, ‘tr’).map do |row|  
> parse\_html(row,‘td’).map do |cell|  
> cell.inner\_html.gsub(%r{\<.\*?\>},“”)  
> end  
> end  
> end
> 
> > ```
> > puts "#{"\t" * tab}}"
> > end
> > n += 1
> > 
> > ```
> > 
> > end  
> > end
> 
> 1. Array#each\_with\_index will keep track of n for you.
> 2. Array#inspect or Kernel#p will print the array in a readable format.
> 
> Otherwise, I think we need more information.  
> Devin

I am new to hpricot, but I tried this  
require ‘watir’  
require ‘hpricot’  
require ‘open-uri’  
page = Hpricot(open(“[http://kvcrpf.org/achievements.htm](http://kvcrpf.org/achievements.htm)”))  
def parse\_html(e1,tag)  
e1.search(“//#{tag}}”)  
end

output = parse\_html(page, ‘table’).map do |table|  
parse\_html(table, ‘tr’).map do |row|  
parse\_html(row,‘td’).map do |cell|  
cell.inner\_html.gsub(%r{\<.\*?\>},“”)  
end  
end  
end

parse\_nested\_array(output[2][4])

By running this code, I get the following error:  
c:/ruby/lib/ruby/gems/1.8/gems/hpricot-0.4-mswin32/lib/hpricot/traverse.rb:110:in  
`search': undefined method `[]=’ for #MatchData:0x2e25624  
(NoMethodError)  
from test1.rb:30:in `parse\_html’  
from test1.rb:33

Please help me in solving this. Also, what should be the approach to  
parse data from an open page, i.e. is there is something like page =  
Hpricot(attach(“[http://kvcrpf.org/achievements.htm](http://kvcrpf.org/achievements.htm)”))

Thanks for your help  
Vikash
