in reply to Display shortened paragraph
Here's an off-the-cuff version that uses regular expressions:
#!/usr/bin/perl use strict; use warnings; my $str = qq(just wondering how i can have perl display part of my lon +g memo. basically i want the first lets say 255 charachters of the p +aragraph. im really new to perl so i don't know how i would come abo +ut this?); my $max = 50; (my $copy = $str) =~ s/(.{1,$max})\b.*/$1.../; print "$copy\n"; __END__
What it does is match a maximum of $max characters and stores that in $1, a word boundary, then the rest of the string and essentially replaces the entire string with whatever it matched in $1 and an ellipsis. Matching the word boundary is nice so that you don't chop off the text in the middle of a word.
Update: Here's another way that does essentially the same thing:
my ($short) = $str =~ m/(.{1,$max}\b)/; $short .= '...';
This is probably how I would do it in my code, I just happened to think of the other way first for some reason.
|
|---|