A
Anno Siegel
John W. Krahn said:Huub said:Codesample: s/(?=([\w\b\]{3}))[\w\b]{1}/(\1)/g
Input: this is a test for fun
Desired output: this is a is a test a test for test for fun for fun
Apologies. I did not realize that random string of words represented
both your input and output.
I still, however, don't understand what you're trying to do. In
precisely what manner does the output relate to the input? It looks
like your output has random pieces of the input interspersed into the
input itself. You need to define how that output is generated.
Paul Lalli
What I'm trying to do is read 3 words, print the 3 words, loose the 1st
word, read the 4th word, print the 3 words, loose the new 1st word, read
the new 4th word, print the new 3 words, etc. What the script does is
basically the same, but for letters. Sofar I can't figure out how to do
it with words.
$ perl -le'
$_ = q/this is a test for fun/;
print;
s/(\w+)(?=(\W+\w+\W+\w+))/$1$2/g;
print;
'
this is a test for fun
this is a is a test a test for test for fun for fun
The regex solutions in this thread are impressive, but in actual code
I'd use a combination of split() and array handling. That deals with
two partial problems separately: splitting the original string into
words (or characters, or whatever), and generating groups of three
from a list:
$_ = 'this is a test for fun';
my $n = 2; # one less than the number of words per group
my @l = split();
my @res = join ' ' => map @l[ $_ .. $_ + $n] => 0 .. $#l - $n;
print "@res\n";
This doesn't generate groups of less than three at the end. If that
is desired one could probably add one or more empty strings to @l.
Anno