Thanks to everyone for the help. The various responses to my question showed me that the perl creed is not idle chatter. There truly is "more than one way to do it!"
Thanks to all, Ryan -----Original Message----- From: Rob Dixon [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 12, 2003 5:36 PM To: [EMAIL PROTECTED] Subject: Re: String manipulation problem [EMAIL PROTECTED] wrote: > Hi, > > While reading each line in a file, If I find a certain number, I need > to increment a digit within that number. For example, when reading > through my file, I expect to find the following line. > > #define DS_FILEVERSION 7,0,0,0462 > > What I want to do is have the ability to increment the first two > zero's after the 7 on demand. I may accasionaly want to increment the > first zero, making the number 7,1,0,0462, or a I may want to just > increment the second zero, making the number 7,0,1,0462 > [snip code] Hi Ryan. This will do what you want. It works by using the m/../g operator which keeps track of the place it last matched and carries on from there. The first match serves both to verify that the line is one that needs altering and to place the search string after the space following the DS_FILEVERSION symbol. The second passes over the first set of digits followed by a comma, leaving the search at the first of the two possible replacements. This match is repeated if it is the second value that needs incrementing. Finally the actual substitution uses the \G anchor which anchors the replacement after the last matched string. I have written the code to simply alternate between incrementing the first and second number. HTH, Rob #perl use strict; my $second; while (<DATA>) { next unless /DS_FILEVERSION\s/g; m/\d+,/g; m/\d+,/g if $second; s/\G(\d+)/$1+1/e; print; $second = not $second; } __DATA__ #define DS_FILEVERSION 7,0,0,0462 #define DS_FILEVERSION 7,0,0,0462 #define DS_FILEVERSION 7,0,0,0462 #define DS_FILEVERSION 7,0,0,0462 output: #define DS_FILEVERSION 7,1,0,0462 #define DS_FILEVERSION 7,0,1,0462 #define DS_FILEVERSION 7,1,0,0462 #define DS_FILEVERSION 7,0,1,0462