# Variable score

**URL:** <https://www.ruby-forum.com/t/variable-score/95553>\
**Category:** Ruby\
**Created:** [December 6, 2005, 11:16pm UTC](https://www.ruby-forum.com/t/variable-score/95553 "2005-12-06T23:16:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 6, 2005, 11:16pm UTC](https://www.ruby-forum.com/t/variable-score/95553/1 "2005-12-06T23:16:15Z")

</div>

Hi all,  
I’m newbie to Ruby. I’m trying to write a small script to parse a text  
file

#-----------------------------------------------------------------------------------  
regexp1 = …  
regexp2 = …

data.each do |line|  
if regexp1.match(line) then  
var1 = $~[1]  
var2 = $~[2]  
end

if regexp2.match(line) then  
var3 = $~[1]  
print “#{var1} #{var2} #{var3}\n”  
end

end  
#-------------------------------------------------------------------------------------  
I can not access to var1 & var2 in 2nd if … then … end control.  
Any idea for help please,

Thank you in advance,

---

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 6, 2005, 11:24pm UTC](https://www.ruby-forum.com/t/variable-score/95553/2 "2005-12-06T23:24:27Z")

</div>

On Dec 6, 2005, at 2:12 PM, removed\_email\_address@domain.invalid wrote:

> Hi all,  
> I’m newbie to Ruby. I’m trying to write a small script to parse a text  
> file
> 
> ## #---------------------------------------------------------------------
> 
> regexp1 = …  
> regexp2 = …

var1 = ‘’ #initialize to striung or whatever else you expect the  
result of the match to be.  
var2 = ‘’

> ## end #---------------------------------------------------------------------
> 
> I can not access to var1 & var2 in 2nd if … then … end control.  
> Any idea for help please,
> 
> Thank you in advance,

HTH-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 6, 2005, 11:32pm UTC](https://www.ruby-forum.com/t/variable-score/95553/3 "2005-12-06T23:32:39Z")

</div>

removed\_email\_address@domain.invalid wrote:

> var1 = $~[1]  
> I can not access to var1 & var2 in 2nd if … then … end control.  
> Any idea for help please,
> 
> Thank you in advance,

Are you certain that you’ve matched regexp1 before you’ve matched  
regexp2?

Regards,  
Matthew

---

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 6, 2005, 11:28pm UTC](https://www.ruby-forum.com/t/variable-score/95553/4 "2005-12-06T23:28:36Z")

</div>

But this is not true, the variables should be visible on the scope of  
this block, even if they are first created inside an “if”. no?

---

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 6, 2005, 11:44pm UTC](https://www.ruby-forum.com/t/variable-score/95553/5 "2005-12-06T23:44:51Z")

</div>

On Dec 6, 2005, at 2:27 PM, ako… wrote:

> But this is not true, the variables should be visible on the scope of  
> this block, even if they are first created inside an “if”. no?

Ahh yes you are right. I misread the source.

-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 7, 2005, 8:16am UTC](https://www.ruby-forum.com/t/variable-score/95553/6 "2005-12-07T08:16:23Z")

</div>

removed\_email\_address@domain.invalid schrieb:

> if regexp2.match(line) then  
> var3 = $~[1]  
> print “#{var1} #{var2} #{var3}\n”  
> end
> 
> end  
> #-------------------------------------------------------------------------------------  
> I can not access to var1 & var2 in 2nd if … then … end control.  
> Any idea for help please,

Do regexp1 and regexp2 match on the same line of data? Or could it be  
that regexp1 matches first and then regexp2 on a following line? If so,  
you have to create var1 and var2 outside of the block, as Ezra has  
shown. Otherwise they are (re)initialized each time the block is  
executed.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![vnpenguin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@vnpenguin](https://www.ruby-forum.com/u/vnpenguin)\
**Post date:** [December 7, 2005, 12:13pm UTC](https://www.ruby-forum.com/t/variable-score/95553/7 "2005-12-07T12:13:16Z")

</div>

Hi,

as others have pointed out, create a reference to var1,var2 before the  
block:

# same as your example:

%w( foo bar ).each do |w|  
if w.match(/foo/)  
a=“something”  
end

if w.match(/bar/)  
puts a #=\> nil  
end  
end

# --------------------------------------------------

# this one is OK

a=nil  
%w( foo bar ).each do |w|  
if w.match(/foo/)  
a=“something”  
end

if w.match(/bar/)  
puts a # =\> something  
end  
end

# --------------------------------------------------

But this is still dangerous, it might lead to hard to find bugs.

Patrick
