CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Truncating data



Apr 16, 2005, 9:16 AM

Post #1 of 3 (1051 views)
Truncating data Can't Post

How can I have a message, such as in a message board, truncated to the first 4-6 words or X characters, whichever is smaller. If a single string is used with no spaces, it could destroy the table with a huge string. I have searched around but have been unable to find much on this subject.
Wink Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


Apr 16, 2005, 11:23 AM

Post #2 of 3 (1050 views)
Re: [benn600] Truncating data [In reply to] Can't Post

use the substr() and length() functions. Here is one way:

my $string = "thisisalongcontinuousstringofwords"; 
my $n = length($string);
substr($string,20,$n,'...') if ($n>20);
print $string;

If the string is longer than 20 characters, everything after 20 characters is removed and replaced with "...". Adjust to your needs. You might also want to consider inserting a line break instead of truncating, or using overflow in your html tables.

<table style="overflow: auto;">

Thaumaturge / Moderator

Apr 17, 2005, 10:02 AM

Post #3 of 3 (1047 views)
Re: [benn600] Truncating data [In reply to] Can't Post

The Text::Wrap module is a standard part of the Perl distribution. It reformats text to insert linebreaks at word breaks so that no line is more than X character long. You could use that and then just grab the first line.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives