Say I wanted to do this:
$str =~ s/foo/bar/g;
$str =~ s/bar/foo/g;
I would obviously have a problem, because a "foo" in the original would be changed back to a "foo". by the second replacement.
The way I do it manually in a text/WP app is:
$str =~ s/foo/%%%/g; # or any other extremely unlikely string
$str =~ s/bar/foo/g;
$str =~ s/%%%/bar/g;
but is there a better way to do it?
$str =~ s/(foo|bar)/$1 eq 'foo'?'bar':'foo';/ge;
works, and benchmarks better than the other way (string with 1,000 chars). Anything else monks can suggest?
percentage_way: 130 wallclock secs (126.01 usr + 0.29 sys = 126.30 CP
+U) @ 7917.66/s (n=1000000)
rhs_way: 49 wallclock secs (46.50 usr + 0.13 sys = 46.63 CPU) @ 21
+445.42/s (n=1000000)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.