The following code:
def mymatch(a,b)
a =~ b
p $1
end
mymatch(/(e…)/,‘hello’)
p $1
returns “ell” and nil. Is there any way to make the value of $1 persist
when the function returns?
martin
The following code:
def mymatch(a,b)
a =~ b
p $1
end
mymatch(/(e…)/,‘hello’)
p $1
returns “ell” and nil. Is there any way to make the value of $1 persist
when the function returns?
martin
“Martin DeMello” martindemello@yahoo.com wrote in message
news:Qd3G9.10876$Qr.203053@news3.calgary.shaw.ca…
returns “ell” and nil. Is there any way to make the value of $1 persist
when the function returns?
From the Pickaxe book:
“$1 to $9 - String - The contents of successive groups matched in the last
successful pattern match. In “cat” =~/(c|a)(t|z)/, $1 will be set to a'' and $2 to
t’'. This variable is local to the current scope.”
Looks like you have to save them explicitly yourself.
Paul
“Martin DeMello” martindemello@yahoo.com wrote in message
news:Qd3G9.10876$Qr.203053@news3.calgary.shaw.ca…returns “ell” and nil. Is there any way to make the value of $1 persist
when the function returns?From the Pickaxe book:
“$1 to $9 - String - The contents of successive groups matched in the last
successful pattern match. In “cat” =~/(c|a)(t|z)/, $1 will be set toa'' and $2 to
t’'. This variable is local to the current scope.”
Oops - should have read the Pickaxe more carefully before asking on the
group. Thanks for the quick reply.
Looks like you have to save them explicitly yourself.
Yeah, but I can’t figure out a way to make them local to the calling
scope, the way an actual Regexp#match call would.
martin
Paul Melis paul@floorball.nl wrote:
“Martin DeMello” martindemello@yahoo.com wrote in message
news:NX3G9.135966$ka.3103955@news1.calgary.shaw.ca…
Yeah, but I can’t figure out a way to make them local to the calling
scope, the way an actual Regexp#match call would.
Why not simply return the MatchData object from your function?
Paul
I do, but I thought it’d be nice to have the $n variables set as well. I
suppose one shouldn’t be allowed to implicitly set variables in the
calling scope, though, now that I think about it.
martin
Paul Melis paul@floorball.nl wrote:
“Martin DeMello” martindemello@yahoo.com wrote in message
news:NX3G9.135966$ka.3103955@news1.calgary.shaw.ca…Yeah, but I can’t figure out a way to make them local to the calling
scope, the way an actual Regexp#match call would.Why not simply return the MatchData object from your function?
Hi,
In message _qaG9.12613$Qr.238759@news3.calgary.shaw.ca,
“Martin DeMello” martindemello@yahoo.com wrote in message
news:NX3G9.135966$ka.3103955@news1.calgary.shaw.ca…Yeah, but I can’t figure out a way to make them local to the calling
scope, the way an actual Regexp#match call would.Why not simply return the MatchData object from your function?
I do, but I thought it’d be nice to have the $n variables set as well. I
suppose one shouldn’t be allowed to implicitly set variables in the
calling scope, though, now that I think about it.
Those $n variables are derived from $~ , the local MatchData object.
You can assign your MatchData object to it and get $n variables.
The following code:
def mymatch(a,b)
a =~ b
p $1
$~
end
match = mymatch(/(e…)/,‘hello’)
p $1
$~ = match
p $1
prints “ell”, nil, “ell”.
Hope this helps.
Martin DeMello martindemello@yahoo.com wrote:
Paul Melis paul@floorball.nl wrote:
–
KUSUNOSE Toru mailto:kusunose@hcn.zaq.ne.jp