Your main loop had a couple of features that struck me as odd: the anchoring of the pattern, and the destruction of one string to build another. The if structure also had redundant branching (you're either going to tag or you're not, so you only need one if).

In pondering how I'd do it, I discovered why you destroy the string and create a new one: walking through the string and changing it becomes complicated. You end up getting lost. (The anchor and the if-structure points are still valid.)

By working from the end of the string, and indexing from the front (or vice-versa), you can insert into the string and not lose your place. A c-style for loop is handy for this:

for (my $pos = length($text); $pos ; --$pos) { my $char = substr($text, $pos-1, 1); if ($char =~ /\S/ and (int rand 2)) # 0 or 1 { substr($text, $pos-1, 1) = "$opening_tag$char$closing_tag"; } }
Another technique that came to mind is the one-line main loop:
$text =~ s/(\S)/(int rand 2) ? "$opening_tag$1$closing_tag" : $1/ge;
Trying to use a while(/\S/g) gets very messy. I was not able to work it out.

Then I started thinking about randomly opening and closing the tag, so you don't end up having to clean up closes followed immediately by opens.

With a toggle, you're always going to get about 50% of the characters tagged; the random number just determines how often you go from tagged to untagged. We need to walk forward through the string, so we'll index from the end:

my $tag_is_open = 0; for (my $pos = length($text); $pos > 0; ) { my $char = substr($text, -$pos, 1); if ($char =~ /\S/ and (int rand 2)) # 0 or 1 { if ($tag_is_open) { substr($text, -$pos, 1) .= $closing_tag; $pos-=2; # After closing a tag, skip at least one char } else { substr($text, -$pos, 0) = $opening_tag; } $tag_is_open ^= 1; # Toggle } else { --$pos } } $text .= $closing_tag if $tag_is_open;
Have you ever appended to substr()? I haven't, before, but it works.


The PerlMonk tr/// Advocate

In reply to Re: CUFP: Random tag insertion by Roy Johnson
in thread CUFP: Random tag insertion by William G. Davis

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.