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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|