Hi Monks,

I want to perform a function on every 'word' found in a string.

Basically pass each word through a function which randomly muddles up the middle letters of a word.

I have found 69688 that looks like it will do what I want.. however I dont get the same result as the example suggests:

use strict; my $test = "This is a sample string of text"; my $tost = $test; $test =~ s/(\w)(\w+)(\w)/$1&garble($2)$3/g; print "ONE: $test\n"; print 'TWO: '.&garble($tost); sub garble { # garbles the middle of a word leaving the first and last characters + alone eg hello -> hlelo my $ret = ''; my @txt = split(/\s/,$_[0]); foreach my $word (@txt){ if($word =~ m/(\w)(\w+)(\w)/){ my $start = $1; my $mid = reverse $2 ; my $end = $3; $ret .= $start.$mid.$end.' '; }else{ $ret .= $word.' '; } } return $ret; } # ONE: T?{&garble(hi)}s is a s?{&garble(ampl)}e s?{&garble(trin)}g of +t?{&garble(ex)}t # TWO: Tihs is a slpmae snirtg of txet
And yes, I am simply reversing the middle letters not randomly jumbling them.. again I am trying to find a nice clean way of doing this.

Any thoughts?

___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com

In reply to Executing a function within a regex by wolis

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.