Fella Monks...
The code below is a demo of my attempt at
writing a little function to retreave N first
sentences from a string. I wonder if there
would be any suggestion on your part as to
other implementations to achieve similar results?
Cheers,
c0d34w4y
my $s = "foo. bar. foobar. foo bar.";
my $p = get_nsentences($s,2); # $p = "foo. bar.";
print $p;
print "done\n";
## SUBS
# get n first sentences from a given string.
sub get_nsentences {
my $source_txt = $_[0];
my $ret_txt = "";
# i know this looks twisted...
for($i=$_[1]||1;$i>0 && length($source_txt);$i--) { $source_txt =~
+ s/([^\.]+\.)/(($ret_txt.=$1)&&"")/e; }
return $ret_txt;
}
--
print join(" ", map { sprintf "%#02x", $_ }unpack("C*",pack("L",0x1234
+5678)))
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.