Hullo,
I've been reading an online tutorial that illustrates a simple substitution of "them" for "us" in the following string:
$_='Us ? The bus usually waits for us, unless the driver forgets us.';
The tutorial's solution is the following:
$_='Us ? The bus usually waits for us, unless the driver forgets us.';
print "$_\n";
s/\b([Uu])s(\W)/chr(ord($1)-1).hem.$2/eg;
print "$_\n";
This works, but when I got to thinking about it...
$_='Us? The bus usually waits for us, unless the driver forgets us.';
print "$_\n";
s/\bus\b/them/ig;
print "$_\n";
also works and seems simpler to a newbie like me.
I guess I'm just wondering if there's something about substitutions that I don't know yet which makes the simpler solution 'bad practice' or sloppy programming? I get the sense that the tutorial chose the solution it did based on something I don't yet know about regex's, but I can't figure out what that is.
Any guidance greatly apreciated.
-d4vis
#!/usr/bin/fnord
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.