LPM: regexp question
llang at baywestpaper.com
llang at baywestpaper.com
Tue Dec 21 08:54:06 CST 1999
llang at baywestpaper.com wrote:
> #======================================
> $sent="This is a test of the first five words.";
> @s=split(/ /,$sent);
> pop(@s) while (@s>5);
Could potentially take a VERY long time. What if $sent is the complete
works of Shakespeare? Better to unshift for (1..5), I would think. Or
join @s[0..4]
> if (join(" ", at s) =~ /\bis|are\b/) {
> print "Do stuff here...";
> }
Hmm. So combining yours and mine ...
if ( join " " (split (/\s+/, $sent, 5) ) ) =~ /\bis|are\b) {
print "Do stuff here ... ";
}
:-)
I've been playing with the numeric as the third argument to split because
I've never seen it before. My book has no mention of it. (Guess I need a
bigger book ;-) )
$sent="This is a test of the first five words.";
@s=split(/ /,$sent,5);
foreach (@s) {print;print"\n";}
yields
This
is
a
test
of the first five words.
The fifth element has everything from that point on. (Might be a Win32-ism
versus other platforms, but I can't see why...)
So it should be pretty quick to change it to 6 and pop the last one off, no
matter what the size of $sent. Or am I shootin' blanks?
Loren
More information about the Lexington-pm
mailing list