"single character substitutions are better done with tr///" is not something I have seen documented anywhere. Is this a question of experience, or something I should have found for myself?

Just adding on to BrowserUk's comments re: tr. Another big factor is that tr doesn't have to worry about the string getting longer! You can't substitute one character with 2 others. But that means that tr doesn't have any memory allocation worries (getting shorter is a whole different deal than getting longer). The net result of all of these simplifications means that tr runs like a rocket.

Update: This thread about tr got me thinking... I volunteer as a TA for a MASM (Microsoft Assembly) class at a local college. We are always thinking of new labs. A "tr" lab is likely to appear in the Fall 2016! The C version of tr is fast, the assembly language version will be really, really fast. And we can teach some other stuff along the way. Quoting my prof for the suggestion of a tr lab:

Implementing tr is a good exercise! It makes use of the optimized array instructions and it reinforces the idea that characters are just integers, which, as you recall from the last ASM class, some students had a hard time converting a digit to its ASCII character. Thanks!

Sometimes these Perl questions spawn other thoughts. Have no doubt that tr can be implemented very efficiently in ASM class 101. That is definitely not true of regex!


In reply to Re^3: Combining regexes by Marshall
in thread Combining regexes by davies

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.