# Re: Problem modifying captured regexp results

**URL:** <https://www.ruby-forum.com/t/re-problem-modifying-captured-regexp-results/73022>\
**Category:** Ruby\
**Created:** [October 20, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/re-problem-modifying-captured-regexp-results/73022 "2006-10-20T18:16:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/re-problem-modifying-captured-regexp-results/73022/1 "2006-10-20T18:16:31Z")

</div>

From: Paul van Delst [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> So can someone elaborate on why the above split operation on  
> captured regexp results seems  
> to bugger up the other captured results? Does this issue  
> extend to _any_ operation on  
> captured regexp results?

That’s what you get for using the (thread-local) global variables that  
change with each regexp operation.

Regexp.match returns a MatchData object that will allow you to save your  
results from a previous operation and not have them changed by later  
code.

See the $~ variable,  
[http://phrogz.net/ProgrammingRuby/tut\_stdtypes.html#objectorientedregula](http://phrogz.net/ProgrammingRuby/tut_stdtypes.html#objectorientedregula)  
rexpressions  
and  
[http://phrogz.net/ProgrammingRuby/ref\_c\_matchdata.html](http://phrogz.net/ProgrammingRuby/ref_c_matchdata.html)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/re-problem-modifying-captured-regexp-results/73022/2 "2006-10-20T18:40:39Z")

</div>

Gavin K. wrote:

> Regexp.match returns a MatchData object that will allow you to save your  
> results from a previous operation and not have them changed by later  
> code.

Hello,

I agree with what you’re saying. I actually have already switched to  
MatchData objects  
rather than the $-variables – for all the usual reasons.

So, while my initial question may be less interesting now (since I have  
working code), I’m  
still curious why in the statement

arrayList\<\<{“type”=\>$1,  
“param”=\>$2,  
“dimlist”=\>$3.split(/\s\*,\s\*/),  
“name”=\>$4,  
“description”=\>$5}

operation on $3 modifies $4 and $5? (And, similarly, operation on $1  
would “clear” $2 to $5)

Based on your first sentence, I guess I have an updated question:

Does using the split method on a $-variable count as a “regexp  
operation”?

cheers,

paulv
