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.
38944d5653a9e373103aaa7ff8562d8a?d=identicon&s=25 vnpenguin (Guest)
on 2005-12-06 23: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,
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 ezra (Guest)
on 2005-12-06 23:24
(Received via mailing list)
On Dec 6, 2005, at 2:12 PM, vnpenguin@gmail.com 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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
A70b7da5a3a712e800100e61ef8d8917?d=identicon&s=25 akonsu (Guest)
on 2005-12-06 23: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?
0817571d150afead454f4220007042fe?d=identicon&s=25 desmarm (Guest)
on 2005-12-06 23:32
(Received via mailing list)
vnpenguin@gmail.com 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
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 ezra (Guest)
on 2005-12-06 23: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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 pit (Guest)
on 2005-12-07 08:16
(Received via mailing list)
vnpenguin@gmail.com 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
3522d767bf5fe5ed27719deb6a88adf7?d=identicon&s=25 clr9.10.randomuser (Guest)
on 2005-12-07 12: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.