On Mon, Nov 2, 2009 at 7:41 PM, Remy Guo <rollingst...@gmail.com> wrote:
> hi folks, > i've got problem when trying to perform a substitution. > > the text file i want to process is like this: > ... > XXXXXX { > ABDADADGA > afj*DHFHH > } (a123) > XXDFAAF { > af2hwefh > fauufui > } (b332) > ... > > i want to match the content in the parenthesis (a123 and b332, here) and > replace them with a sorted number (0;0, 0;1, 0;2, .... 0; 255, 1;0, > 1;1, ...1; 255, for example), so i wrote the script like this: > if (m/\}.*\(.*\)/) > { > $j++; > if ($j >= 255) > { > $i++; > $j = 0; > } > > $_ =~ s/\(.*\)/$i;$j/ > } > > now the problem is that, i'm sure the matching > regex successfully matches the line that i want, but the subsitution just > don't happen. why?... > need your help....thanks! > > - Remy > First, the s/// operator applies to $_ by default. Second, as far as I can tell there's no indication anywhere in your script as to what the value of $_ might be. This works for me: use strict; use warnings; open(my $INFILE, "<", "in.txt"); open(my $OUTFILE, ">", "out.txt"); my $line; my $i = 0; my $j = 0; while ($line = <$INFILE>) { if ($line =~ /\((.*?)\)/) { $line =~ s/$1/$i;$j/; $j++; if ($j > 255) { $j = 0; $i++; } } print $OUTFILE $line; }