Given the two updates,
LTjake certainly has the approach I would
take, but I think the version I saw would end up
scanning the entire input post, rather than quiting as soon as
the output string is done. Here's the way the while loop
was written when I first saw it (with my commentary added):
while ( my $token = $p->get_token ) {
if ($token->is_text) {
if (length($token->return_text) + $total <= 200) {
$doc2 .= $token->return_text;
$total += length($token->return_text);
}
else {
for (split / /, $token->return_text) {
if ($total + length($_) <= 200) {
$doc2 .= $_ . ' ';
$total += length($_) + 1;
}
else {
last; ## THIS ONLY EXITS THE FOR LOOP
} ## So this block runs over the
} ## entire remainder of the post
chop($doc2) if $doc2 =~ /\s$/;
}
}
else {
$doc2 .= $token->as_is;
}
}
The solution would be to add the length test to the while
loop condition, or else figure a way to avoid an inner
for loop, so that "last" will really finish things off.
And some other nit-picks:
- Rather than calling the same "return_text" method so
many times, I'd rather use it once to set a local variable (and
having just read the TokeParser::Simple docs, it seems that
"return_text" is depricated anyway -- use "as_is" in all cases).
- The for loop uses a split on single space -- not what
the OP intended -- but this
leads to a question about the OP's stated goals:
Would you want every input string of /\s{1,}/
to be represented (and counted) as a single space in the
output string? (Myself, I would think "yes".)
So here's my version of LTjake's while loop (not
tested):
while ( my $token = $p->get_token )
{
my $tkntext = $token->as_is;
$tkntext =~ s/\s+/ /g; # normalize all whitespace
if ($token->is_text) {
if (length($tkntext) + $total <= 200) {
$doc2 .= $tkntext;
$total += length($tkntext);
}
else {
my $maxlen = 200 - $total;
$doc2 .= substr( $tkntext, 0, rindex( $tkntext, ' ', $maxl
+en );
last; # this finishes the while loop
}
}
else {
$doc2 .= " $tkntext ";
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.