Click to See Complete Forum and Search --> : Walking Substitution in Perl


1998bmwz3
05-17-2005, 07:44 PM
Hello All:

I am modifying contents of a txt file that looks something like this:

c1U5 := c1G(011001100110011001100110011001100110011001100110
1001100110011001100110011001100110011001100110011001100110
1100110011001100110011001100110011001100110011001100110011

What I want to do is replace all the 1's after the "(" with 0's.

The global substitution
s/1/0/g;
does not work because the "c1U5" & "c1G" in line 1 get modified to "c0U5" & "c0G".

So essentially what I want to do is walk along the string till I see "(", and then start replacing all the following 1's by 0's.

Please let me know the best way of doing this.

Appreciate your time!!

Nedals
05-18-2005, 02:54 AM
Do it in 2 steps...

use strict;
my $data = "c1U5 := c1G(011001100110011001100110011001100110011001100110
1001100110011001100110011001100110011001100110011001100110
1100110011001100110011001100110011001100110011001100110011";

my($prefix,$code) = split('\(',$data);
$code =~ s/1/0/g;
print "$prefix($code";

1998bmwz3
05-18-2005, 04:26 PM
Thanks a lot Nedals. That works.