wolis has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.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
Any thoughts?
___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Executing a function within a regex
by ChemBoy (Priest) on May 10, 2004 at 02:31 UTC | |
|
Re: Executing a function within a regex
by graff (Chancellor) on May 10, 2004 at 02:53 UTC | |
|
Re: Executing a function within a regex
by zude (Scribe) on May 10, 2004 at 03:20 UTC | |
|
Re: Executing a function within a regex
by BrowserUk (Patriarch) on May 10, 2004 at 03:11 UTC | |
by wolis (Scribe) on May 10, 2004 at 04:04 UTC |