Hey, for once I'm going to actually put some code on one of these darn things!!
use warnings;
use strict;
my $text = "I'd like a foobar(s)! What about you?";
print "Pretext: $text\n";
my $num_of_chars = 13;
$text =~ s/(.{$num_of_chars}[\w|\(.*\)\w]*).*/$1/;
print $text;
OUTPUT:
Pretext: I'd like a foobar(s)! What about you?
I'd like a foobar(s)
Now, all you'd really need to do is expand on the last part to correctly handle which punctation you want to handle for the last part.
here's my second try if you want word counts
use warnings;
use strict;
my $text = " I'd like a foobar(s)! What about you?";
print "Pretext: $text\n";
my $num_of_words = 6;
my @text;
for (my $i=1; $i<=$num_of_words; $i++) {
$text =~ s/^\s*([\w|\(|\)|'|:|!|\.|\,|;]*\s*)(.*)/$2/;
push (@text,$1);
}
for (@text) { print $_; }
OUTPUT:
Pretext: I'd like a foobar(s)! What about you?
I'd like a foobar(s)! What about
This is just taking the first word, regardless of puctuation, which may cause some grief, and the next space(s), and putting it in an array. A good check to put in there would be that if $text becomes undef to stop looping. Also, if you're reading in from a file line by line, what I'd do is get the first few lines and concatenate them together to form your text to get the data from if you're going over say, 50 or so words.
Please note that I'm nowhere near the level of others here and the code above is laughable by perl monk standards, but it should help you get along. :)
UPDATE: Updated code a bit.
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.