--- David Draley <[EMAIL PROTECTED]> wrote:
> How would I print only the words that contain the letter "p" in a file???
> Right now I am only printing the entire lines that contain words with the
> letter "p".
>
> while (<FILE>)
> {
> if(/[p]/)
> {
> print "$_";
> }
> }
> close(FILE);
>
> thanks in advance
Assuming you only want a lower-case 'p', you can try this:
use strict;
use warnings;
while (<DATA>) {
my $wordchars = q|[-'\w]|;
while ( /\b($wordchars*p$wordchars+|$wordchars+p$wordchars*)\b/g ) {
print "$1\n";
}
}
__DATA__
Use Perl.
Perl is appealing.
Use it to write a killer app.
If you want an upper-case 'P' to be included, change the regex to:
/\b($wordchars*[Pp]$wordchars+|$wordchars+[Pp]$wordchars*)\b/
The $wordchars variable is a character class that defines what characters are allowed
in "words".
Cheers,
Curtis "Ovid" Poe
=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/
__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger.
http://im.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]