[Newbie] Variable score

V

vnpenguin

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,
 
E

Ezra Zygmuntowicz

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

#---------------------------------------------------------------------
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,

Thank you in advance,

HTH-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
(e-mail address removed)
 
A

ako...

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?
 
M

Matthew Desmarais

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,
Are you certain that you've matched regexp1 before you've matched regexp2?

Regards,
Matthew
 
E

Ezra Zygmuntowicz

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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
(e-mail address removed)
 
P

Pit Capitain

#-----------------------------------------------------------------------------------
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,

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
 
P

Patrick Gundlach

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top