# dividing strings

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I'm sorry I couldn't post code for this question, but I have no idea
where
to start on this one.

I need code (or suggestions) for the problem of inserting a string into
a
variable string every x (number) characters and then parsing it back
into
the first original string and then printing the original string with
the
inserted strings.

Thanks,

--
Robin
--
webmaster@infusedlight.net

## Re: dividing strings

thanks for all the help. I really appreciate it, the code looks great.

## Re: dividing strings

> I need code (or suggestions) for the problem of inserting a string into
> a variable string every x (number) characters and then parsing it back
> into the first original string and then printing the original string with
> the inserted strings.

How doy ou propose to deal with:

my \$src = 'a' x 10;
my \$insert_every_third_character = 'a';

The resulting string will be:

'aaaaaaaaaaaaa'

How are you going to take out the a's?

At least making an attempt to clearly state a question can do wonders in

Sinan.

## Re: dividing strings

> I'm sorry I couldn't post code for this question, but I have no idea
> where
> to start on this one.
>
> I need code (or suggestions) for the problem of inserting a string into
> a
> variable string every x (number) characters and then parsing it back
> into
> the first original string and then printing the original string with
> the
> inserted strings.
>
> Thanks,
>
>
> --
> Robin
> --
> webmaster@infusedlight.net
>

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"

------------
use strict;
use warnings;

my \$insert="XXX";
my \$string="abcdefghijklmnopqrstuvwxyz";
my \$size=3;

my @chunks;
#break the string into chunks of appropriate size
foreach (\$string=~ /./g){push (@chunks, \$_);}
#put it back together with insert between the chunks
my \$final=join(\$insert,@chunks);
print \$final;

## Re: dividing strings

>> I'm sorry I couldn't post code for this question, but I have no idea
>> where
>> to start on this one.
>>
>> I need code (or suggestions) for the problem of inserting a string into
>> a
>> variable string every x (number) characters and then parsing it back
>> into
>> the first original string and then printing the original string with
>> the
>> inserted strings.
>>
>> Thanks,
>>
>>
>> --
>> Robin
>> --
>> webmaster@infusedlight.net
>>
>
> 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"
>
>
> ------------
> use strict;
> use warnings;
>
> my \$insert="XXX";
> my \$string="abcdefghijklmnopqrstuvwxyz";
> my \$size=3;
>
> my @chunks;
> #break the string into chunks of appropriate size
> foreach (\$string=~ /./g){push (@chunks, \$_);}
> #put it back together with insert between the chunks
> my \$final=join(\$insert,@chunks);
> print \$final;

wow, thanks, I can see what that code is doing, nice array context.
-Robin

## Re: dividing strings

>> I'm sorry I couldn't post code for this question, but I have no idea
>> where to start on this one.
>>
>> I need code (or suggestions) for the problem of inserting a string
>> into a variable string every x (number) characters and then parsing
>> it back into the first original string and then printing the original
>> string with the inserted strings.

....

> 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=~ /./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

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

#! /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

## Re: dividing strings

>
>
> >> I'm sorry I couldn't post code for this question, but I have no idea
> >> where to start on this one.
> >>
> >> I need code (or suggestions) for the problem of inserting a string
> >> into a variable string every x (number) characters and then parsing
> >> it back into the first original string and then printing the original
> >> string with the inserted strings.
>
> ...
>
> > 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=~ /./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
>
> 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
>
>
> #! /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";

You're basically splitting the string into substrings of length at most
\$step and joining them back with \$insert between them.  Perl has high
level functions that do this.  A regex, or unpack, can do the splitting
and join can insert the fixed part:

my \$target = join \$insert, \$source =~ /(.)/g;

or

my \$target = join \$insert, unpack "(a\$dist)*", \$source;

On a different note, running through the insertion points backward, one
can insert the fixed string directly:

\$target = \$source;
substr( \$target, \$_, 0) = \$insert for reverse
map \$_*\$step, 1 .. length( \$target)/\$step;

Anno

## Re: dividing strings

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in

>> 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";
>
> You're basically splitting the string into substrings of length at
> most \$step and joining them back with \$insert between them.  Perl has
> high level functions that do this.

Yes, but I have a primitive mind

....

> On a different note, running through the insertion points backward,
> one can insert the fixed string directly:
>
>     \$target = \$source;
>     substr( \$target, \$_, 0) = \$insert for reverse
>         map \$_*\$step, 1 .. length( \$target)/\$step;

which I can improve by studying this.

Thanks.

Sinan.

## Re: dividing strings

>
>
> >> I'm sorry I couldn't post code for this question, but I have no idea
> >> where to start on this one.
> >>
> >> I need code (or suggestions) for the problem of inserting a string
> >> into a variable string every x (number) characters and then parsing
> >> it back into the first original string and then printing the original
> >> string with the inserted strings.
>
> ...
>
> > 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"

[well-founded critique of original question]

>
> #! /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";

You're basically splitting the string into substrings of length <=
\$step and joining them back with \$insert between them.  Perl has high
level functions that do this.  A regex, or unpack, can do the splitting
and join can insert the fixed part:

my \$target = join \$insert, \$source =~ /./g;

or

my \$target = join \$insert, unpack "(a\$dist)*", \$source;

On a different note, running through the insertion points backward, one
can insert the fixed string directly:

\$target = \$source;
substr( \$target, \$_, 0) = \$insert for reverse
map \$_*\$step, 1 .. length( \$target)/\$step;

Anno

## Re: dividing strings

>I'm sorry I couldn't post code for this question, but I have no idea
>where
>to start on this one.
>
>I need code (or suggestions) for the problem of inserting a string into
>a
>variable string every x (number) characters and then parsing it back
>into
>the first original string and then printing the original string with
>the
>inserted strings.

Robin, my son, I can't understand a word you're saying.  Why not post
some examples of what your start and end points are; maybe some
tolerant person who hasn't killfiled you already (there are a few)
will try to help.

## Re: dividing strings

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Fort Worth, Texas

## Re: dividing strings

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--