Forum Moderators: phranque

Message Too Old, No Replies

Regex substitution within a while() condition

         

csdude55

6:13 pm on Nov 2, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm doing this in Perl, but I think that the question is the same for pretty much any language so I'm putting it in General. If you think it should be under Perl, though, @phranque, I'll understand!

I have this tidbit:

$string = 'first there is the foo, then comes the bar';
$x = 1;

while ($string =~ m{(
foo |
bar
)}xg) {
print "$1 => lorem\n";
$string =~ s/$1/lorem/xg;

print "$x\n";
$x++;
}

print $string;

# Result:
# foo => lorem
# 1
# bar => lorem
# 2
# first there is the lorem, then comes the lorem


Recognizing that this runs 2 expressions for each loop iteration, I thought I might be able to cut it in half with:

while ($string =~ s{(
foo |
bar
)}
{lorem}xg) {
print "$1 => lorem\n";

print "$x\n";
$x++;
}

# Result:
# bar => lorem
# 1
# first there is the lorem, then comes the lorem


The first version appears to find the first match, run the "success" code, then go back and find the second match.

But the second version finds all of the matches, does the substitution, then runs the success code once after the substitutions are all done. Which, of course, defeats the whole purpose of having it in the loop in the first place.

Can you suggest a modification to make the second version run the success code after each successful substitution, rather than only running the success code at the end?

The primary goal here is to speed it up.

lucy24

9:39 pm on Nov 2, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



to make the second version run the success code after each successful substitution
Isn’t that what subroutines or external functions are for? (Actual structure and terminology depending, of course, on language.)

Doesn't the /g flag mean “substitute all of them at once, in one fell swoop, no matter how many there are”?

csdude55

9:56 pm on Nov 2, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Doesn't the /g flag mean “substitute all of them at once, in one fell swoop, no matter how many there are”?

Hahahahaha, well duh! You know I spent 2 hours on this before posting? LOL

csdude55

10:49 pm on Nov 2, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



FYI, though... over 1000 iterations, it turns out that the original was faster :-)

Original: 1.8908s
Modification: 2.3475s