I can’t get a gsub! to work when I use a counter variable in my
replacement.
content.gsub!(/^%%Page:.*([0-9]{1,5})$/, “%%Page: $1”)
I literally get “Page: $1” in my output. Why? How do I indicate that I
want the number, whatever it was, from the search? I’ve tried #{$1}, but
that didn’t work either. RUBY does use $1 for its counters, doesn’t it?
I’ve tried \1, but, that gives me even worse results.
Thanks a lot.
I’ve tried \1, but, that gives me even worse results.
‘%%Page: \1’ or “%%Page: \1” does what you want.
Christoffer S. wrote:
I’ve tried \1, but, that gives me even worse results.
‘%%Page: \1’ or “%%Page: \1” does what you want.
Thank you, Christopher!
Now, could you tell me how to have a counter for that \1? In other
words, I want the numbers to increment, starting from 1 through however
many “%%Page” indicators there are. Is there any way to do that in the
same gsub! line?
Thanks again.
On 5/16/06, Guest [email protected] wrote:
same gsub! line?
Thanks again.
The block form of gsub is one way to go about it:
counter = 0
content.gsub!(/^%%Page:.*([0-9]{1,5})$/) do |match|
counter += 1
“%%Page: #{counter}”
end
Whatever the block returns is used to replace the matched text.
Wilson B. wrote:
On 5/16/06, Guest [email protected] wrote:
same gsub! line?
Thanks again.
The block form of gsub is one way to go about it:
counter = 0
content.gsub!(/^%%Page:.*([0-9]{1,5})$/) do |match|
counter += 1
“%%Page: #{counter}”
end
Whatever the block returns is used to replace the matched text.
Cool. Thank you both very much. This is a very generous forum, I must
say.
-Peter
On 5/16/06, Guest [email protected] wrote:
counter += 1
“%%Page: #{counter}”
end
Whatever the block returns is used to replace the matched text.
Cool. Thank you both very much. This is a very generous forum, I must
say.
I wrote that reply prior to coffee, and you’ll note that the |match|
block variable is unnecessary, since you’re not doing anything with
it.
Also, I meant to say “the value of the block”, not “what the block
returns”.
Other than that, it stands up to 6 hours of wakefulness. Heh.