Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to find a number and replace it with calculated value?

by toolic (Bishop)
on Mar 03, 2017 at 13:19 UTC ( #1183562=note: print w/replies, xml ) Need Help??


in reply to How to find a number and replace it with calculated value?

Use the 'e' modifier for s///
use warnings; use strict; while (<DATA>) { s/(Z=)(\d+)/$1 . $2*4/eg; my $i = 1; s/\b(XP)\b/$1 . ' (' . $i++ . ')'/eg; print; } __DATA__ Z=1 Z=2 Z=3 foo XP bar XP boo

Outputs:

Z=4 Z=8 Z=12 foo XP (1) bar XP (2) boo

Replies are listed 'Best First'.
Re^2: How to find a number and replace it with calculated value?
by Lejocode (Novice) on Mar 03, 2017 at 13:33 UTC
    Thanks toolic, that's what i wanted. just a small issue, the increment of occurrence number is per line, how to make it per file or multipleline?
      ... the increment of occurrence number is per line, how to make it per file or multipleline?

      Move the  my $i = 1; statement to before the loop (untested):

      my $i = 1; while (<DATA>) { ... s/\b(XP)\b/$1 . ' (' . $i++ . ')'/eg; print; }


      Give a man a fish:  <%-{-{-{-<

        It did it :) thank you guys.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1183562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-01-20 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?