Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: How do I truncate a string while preserving words?

by Cap'n Steve (Friar)
on May 12, 2007 at 08:02 UTC ( #615052=note: print w/replies, xml ) Need Help??


in reply to Re: How do I truncate a string while preserving words?
in thread How do I truncate a string while preserving words?

Maybe I read it wrong, but it looks like those modules are geared toward displaying all the text, just divided into lines. I wanted to only display the first part, kind of like a preview.
  • Comment on Re^2: How do I truncate a string while preserving words?

Replies are listed 'Best First'.
Re^3: How do I truncate a string while preserving words?
by tomfahle (Priest) on May 12, 2007 at 14:28 UTC
    Try Text::Autformat and split to get the first line.
    use strict; use warnings; use Text::Autoformat; my $string = 'A lengthy text with more than xxx characters to demonstr +ate truncate'; print "Before: $string\n"; print 'Length: ' , length($string) , "\n"; my $truncated = &truncate($string,20); print "After: $truncated\n"; print 'Length: ' , length($truncated) , "\n"; ########################################################### sub truncate { my $string = shift @_; my $length = 75; $length = shift if @_; $string = autoformat($string, { left => 0, right => $length , widow => 0, }); ($string) = split(/\n/,$string); # Just the first element return $string; } ## ###########################################################
    Just my two cents. Hope this helps

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://615052]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2021-11-29 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?