Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:23:57+00:00 2026-06-18T05:23:57+00:00

I am trying to use ruby to generate an iterated value in gsub(). The

  • 0

I am trying to use ruby to generate an iterated value in gsub(). The line I wrote is:

ruby -pe '$i=0; gsub(/⎦\d+?⎡/, "⎦#{$i+=1}⎡")' < test.txt

But that doesn’t actually do the iteration; instead, it sets the value of $i to zero and adds 1 to it each time the substitution takes place.

I have also used:

ruby -pe 'BEGIN{$i=0}; gsub(/\d+?/, "#{$i+=1}")' < test.txt

That one outputs the line number instead of the variable iteration value. Shouldn’t the gsub() get called ONLY when the REGEX has a match?

The purpose of the command is to substitute the iterated value + 1 for the next number found in the file. e.g. :

Lorem ipsum dolor 2 sit amet, consectetuer adipiscing elit, sed 7 diam nonummy nibh euismod 1 tincidunt ut laoreet dolore magna 10 aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci 15 tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Output:

Lorem ipsum dolor 1 sit amet, consectetuer adipiscing elit, sed 2 diam nonummy nibh euismod 3 tincidunt ut laoreet dolore magna 4 aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci 5 tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Update1:
I have tried this line after further reading:

ruby -pe 'BEGIN{$i=0}; gsub /\d+?/, "#{$i+=1}" if ~/\d+?/' < aa01.xhtml

However, it seems that the process in the command lines I used is not correct to start with. It should first find the line which has numbers. Then increment the variable and substitute it in the place of the number found and move to the next number if any are present in the same line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:23:58+00:00Added an answer on June 18, 2026 at 5:23 am

    I think that the expression "⎦#{$i+=1}⎡" is computed only once. A block is computed at each replacement.

    $i=0
    File.open('test.txt') do |f|
        f.each_line do |line|
            modif = line.gsub(/\d+/) {|num| "#{$i+=1}"}
            puts modif
        end
    end
    

    outputs :

    Lorem ipsum dolor 1 sit amet, consectetuer adipiscing elit, sed 2 diam nonummy nibh euismod 3 tincidunt ut laoreet dolore magna 4 aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci 5 tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
    

    New input :

    Lorem ipsum dolor 2 sit amet, consectetuer adipiscing elit, 
    sed 7 diam nonummy nibh 
    euismod 1 tincidunt ut 44
    
    laoreet dolore magna 10 aliquam erat volutpat.
    Ut wisi enim 0 ad minim veniam, 4 quis nostrud 
    exerci 15 tation ullamcorper 66 suscipit 88 
    lobortis 6 nisl ut aliquip ex 1 ea1 co1mmodo consequat.
    

    output :

    Lorem ipsum dolor 1 sit amet, consectetuer adipiscing elit, 
    sed 2 diam nonummy nibh 
    euismod 3 tincidunt ut 4
    
    laoreet dolore magna 5 aliquam erat volutpat.
    Ut wisi enim 6 ad minim veniam, 7 quis nostrud 
    exerci 8 tation ullamcorper 9 suscipit 10 
    lobortis 11 nisl ut aliquip ex 12 ea13 co14mmodo consequat.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use rails generate to generate an integration test by running
I'm trying to generate haml views with devise, but it always use erb instead.
I'm trying to use ruby to generate a PDF using Prawn on OS X.
I'm trying to use this Ruby wrapper https://github.com/mattetti/Pvwatts but I have an issue with
I'm trying to generate a long Latex document with Ruby. If I use a
I'm trying to use RSPec to test my Ruby on Rails 3.2 app. When
background: trying to use the twitter gem for ruby-on-rails. in routes: map.resources :twitter_sessions map.finalize_twitter_sessions
In my ruby on rails app I am trying to use a Prototype Form.Element.Observer
I'm trying to use regex as the conditional in a Ruby (1.9.2) if statement
I'm a beginner coder in Ruby and currently I'm trying to use this plugin:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.