Forum: Ruby Variable score

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
vnpenguin (Guest)
on 2005-12-07 00:16
(Received via mailing list)
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,
ezra (Guest)
on 2005-12-07 00:24
(Received via mailing list)
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
akonsu (Guest)
on 2005-12-07 00:28
(Received via mailing list)
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?
desmarm (Guest)
on 2005-12-07 00:32
(Received via mailing list)
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
ezra (Guest)
on 2005-12-07 00:44
(Received via mailing list)
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
pit (Guest)
on 2005-12-07 09:16
(Received via mailing list)
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
clr9.10.randomuser (Guest)
on 2005-12-07 13:13
(Received via mailing list)
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
This topic is locked and can not be replied to.