dividing strings

R

robin

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,
 
A

A. Sinan Unur

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
terms of your ability to actually answer it.

Sinan.
 
L

LEH

robin said:
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,

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=~ /.{1,$size}/g){push (@chunks, $_);}
#put it back together with insert between the chunks
my $final=join($insert,@chunks);
print $final;
 
H

Henry Law

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.
 
A

A. Sinan Unur

....

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
 
R

Robin

LEH said:
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=~ /.{1,$size}/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
 
A

Anno Siegel

A. Sinan Unur said:
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.


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.


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";

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 =~ /(.{1,$step})/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
 
A

Anno Siegel

[well-founded critique of original question]
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";

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 =~ /.{1,$step}/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
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
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.
 
A

A. Sinan Unur

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.

XXXwrote: 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
 
A

A. Sinan Unur

(e-mail address removed)-dot-invalid.no-spam.invalid (A. Sinan Unur) wrote in

Just for everyone's information, I did *NOT* send this message, although
this does seem to be a verbatim copy of one of my earlier postings.

If this fake was posted with malicious intent, I expect some offensive
posts to come later. Please don't respond to those or report them to
Cornell. I will try to deal with this imposter.

Messages I send have a specific identifier embedded in the message ID, and
I never use readfreenews.net or similar services.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top