[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Substitution



> In a script a coworker of mine is writing, it is necessary to do the
> following:
>
> Substituting a substring ($pat) inside of a big (>500K) string
> ($text), but the first time it is changed to one thing ($s1) and
> afterwards it is changed to another thing ($s2). Also, $s1 contains
> the original $pat (in fact, it is $pat . $s2). So far, the best we've
> been able to come with is:
>
> $text =~ s/$pat/$s1/;
> 1 while ($text =~ s/$pat(.*?)$pat/$pat$1$s2/);
>
> but obviously it is horribly inefficient.
>
> Does someone know of a better/faster way? Is there any one-liner for
> that?

i'll give it a shot, tho it's hard without an example of the "big
string"...how 'bout:

 $text =~ s/$pat/$s1/; # do the first one
 $text =~ s/$pat(?!$s2)/$s2/g; # do the rest
--
@-------------------------------@--------------------@
| Michael Budash Consulting     | (707) 252-7670     |
| Perl, Javascript, PHP, MySQL  | (603) 250-8679 fax |
| Official Extropia Developer   | mbudash@sonic.net  |
@-------------------------------@--------------------@

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org