in reply to Problems with matching?

Turn your while into a s///eg

$file =~ s{^\s+description\s\K(\w{4})(-\w+-\w+)(?:=;)}{ my $prefix = $1; my $suffix = $2; # Optional Transformation logic here, giving $prefix and $suffix n +ew values. $prefix.$suffix }mge;

Replies are listed 'Best First'.
Re^2: Problems with matching?
by Argel (Prior) on May 18, 2011 at 23:32 UTC
    I added the reset later, with no effect. And how would I change the while m// to s///? I need to find what I am substituting first, right? I did update my_rand to deal with duplicates (will update code above).

    Update1; Oh, put code within the substitution!!

    Update2: How do I get it to loop? :/

    Elda Taluta; Sarks Sark; Ark Arks

      if you just want to replace the first occurance of a name, then use the following

      my %new_names; $file =~ s{^\s+description\s\K(\w\w\w\w)(?=-\w+-\w+;)}{ my $name = $1; if (!$new_names{$name}) { $name = $new_names{$name} = my_rand(); } $name }gme;

      Or if you want to replace all occurances of a name with the new name, then the following would work:

      my %new_names; $file =~ s{^\s+description\s\K(\w\w\w\w)(?=-\w+-\w+;)}{ $new_names{$1} //= my_rand(); }gme;

      Note: since the suffix appears to not be relevant, I added it to the positive look ahead assertion.