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,
var1 = '' #initialize to striung or whatever else you expect the result of the match to be.
var2 = ''
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,
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,
Are you certain that you've matched regexp1 before you've matched 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,
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.