Forum: Ruby Problem with my gsub!

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.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-05-16 14:56
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.
A19281bdbc5f08539cdef3d6636f7c4d?d=identicon&s=25 Christoffer Sawicki (Guest)
on 2006-05-16 15:01
(Received via mailing list)
> I've tried \1, but, that gives me even worse results.

'%%Page: \1' or "%%Page: \\1" does what you want.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Guest (Guest)
on 2006-05-16 15:23
Christoffer Sawicki 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.
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-05-16 15:33
(Received via mailing list)
On 5/16/06, Guest <pbailey@bna.com> 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.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Guest (Guest)
on 2006-05-16 15:35
Wilson Bilkovich wrote:
> On 5/16/06, Guest <pbailey@bna.com> 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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-05-16 21:45
(Received via mailing list)
On 5/16/06, Guest <pbailey@bna.com> 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.
This topic is locked and can not be replied to.