....
I would do the following, although there may be a more concise way to
do it. Larry
-- but not sure what you mean by "parsing it back into the first
original string"
Robin seems to want to extract the milk from the pudding once the
pudding is made. Without further conditions, the problem is not well
defined.
use strict;
use warnings;
Good.
my $insert="XXX";
my $string="abcdefghijklmnopqrstuvwxyz";
See how you chose the $insert string so that it did not contain any of
the characters? For the problem to be well-defined, such an assumption,
if it exists, must be specified explicitly.
my $size=3;
my @chunks;
#break the string into chunks of appropriate size
foreach ($string=~ /.{1,$size}/g){push (@chunks, $_);}
#put it back together with insert between the chunks
my $final=join($insert,@chunks);
print $final;
Feel free to use as much whitespace as needed to make it easier for
others to read your post.
Is this what you were trying to achieve?
#! /usr/bin/perl
use strict;
use warnings;
my $insert = 'XXX';
my $source = 'abcdefghijklmnopqrstuvwxyz';
my $step = '.' x 4;
$source =~ s/($step)/$1$insert/g;
print "Transformed: $source\n";
$source =~ s/$insert//g;
print "Source (not really): $source\n";
__END__
D:\Home\asu1\UseNet\clpmisc> s1
ransformed: abcdXXXefghXXXijklXXXmnopXXXqrstXXXuvwxXXXyz
ource (not really): abcdefghijklmnopqrstuvwxyz
How about this one?
#! /usr/bin/perl
use strict;
use warnings;
my $insert = 'XXX';
my $source = 'abcdefghijklmnopqrstuvwxyz';
my $step = 4;
my $target;
my $cursor = 0;
while($cursor < length $source) {
my $chunk = substr($source, $cursor, $step);
unless($step == length $chunk) {
$target .= $chunk;
last;
}
$target .= $chunk . $insert;
$cursor += $step;
}
print "Original: $source\nTransformed: $target\n";
__END__
D:\Home\asu1\UseNet\clpmisc> s
Original: abcdefghijklmnopqrstuvwxyz
Transformed: abcdXXXefghXXXijklXXXmnopXXXqrstXXXuvwxXXXyz
Sinan