Variable score


#1

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,


#2

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
509-577-7732
removed_email_address@domain.invalid


#3

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


#4

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?


#5

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
509-577-7732
removed_email_address@domain.invalid


#6

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


#7

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