Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Replace text in file

by Yaerox (Scribe)
on Apr 17, 2014 at 08:18 UTC ( [id://1082608]=note: print w/replies, xml ) Need Help??


in reply to Replace text in file

Maybe another possible way for you could be using perl native function substr() instead of using regular expressions. Laurent_R already explained those above ;-)

Replies are listed 'Best First'.
Re^2: Replace text in file
by Laurent_R (Canon) on Apr 17, 2014 at 10:44 UTC
    Yes, that could be used, but the problem I see with substr here is that we cannot be sure whether the version number has one or two or several digits, so you need further processing for determining that. To me, a regex substitution is simpler in this context.

      While I probably wouldn't use substr for this, I don't really see the number of (before or after) digits as being a problem. Here's a (multi-line) one-liner to explain:

      $ perl -Mstrict -Mwarnings -E ' my $find = q{#define MYVERSION }; my $flen = length $find; my @strings = ( q{#define MYVERSION 11}, q{#define MYVERSION 9}, q{#define MYVERSION 99} ); for (@strings) { say; substr($_, $flen) = substr($_, $flen) + 1; say; } ' #define MYVERSION 11 #define MYVERSION 12 #define MYVERSION 9 #define MYVERSION 10 #define MYVERSION 99 #define MYVERSION 100

      Anyway, that's all fairly academic: I'm pretty sure I would have reached for something like s/^($find)(\d+)/$1.($2+1)/e in the first instance.

      -- Ken

      "we cannot be sure whether the version number has one or two or several digits"

      Totally agree with you on that point.

      Regards

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found