L
Looking
Here is what I am trying to do:
For a given string, break it into two parts. The first part should be as
long as possible but less than 25 characters. The SPLIT key is a whitespace.
The second part is whatever left in the string;
My algorithm is to break the string to into many small parts the join them
with space.
@parts= split (/\W/, $original);
$thelength=0;$newpart1='';
foreach (@parts) {
$thelength=$thelength+length($_)+1;
if ($thelength > 25) {break;}
else {$newpart1=$newpart1.$_;}
}
I think my algorithm has problem. The simple task should be done within 2
lines of regex.
I am thinking of substr(); but I don't know how to find the position of the
last whitespace before character #25. Or I can do substr($original, 0, 25)
then check if the 25 is a whitespace etc... if not then go to #24.
Both my algorithms take n*n, which is really bad speed.
For a given string, break it into two parts. The first part should be as
long as possible but less than 25 characters. The SPLIT key is a whitespace.
The second part is whatever left in the string;
My algorithm is to break the string to into many small parts the join them
with space.
@parts= split (/\W/, $original);
$thelength=0;$newpart1='';
foreach (@parts) {
$thelength=$thelength+length($_)+1;
if ($thelength > 25) {break;}
else {$newpart1=$newpart1.$_;}
}
I think my algorithm has problem. The simple task should be done within 2
lines of regex.
I am thinking of substr(); but I don't know how to find the position of the
last whitespace before character #25. Or I can do substr($original, 0, 25)
then check if the 25 is a whitespace etc... if not then go to #24.
Both my algorithms take n*n, which is really bad speed.