HollyKing has asked for the wisdom of the Perl Monks concerning the following question:
Ok, be gentle, this is my first attempt at a post. I do want to learn so constructive criticism will be gladly accepted, but pointless flames will be suitably ignored. Also, this is a homework question, but I've already turned in the assignment. I'm asking to improve my skills.
First, the assignment. How would you print the first and last word of every line of input? Create a script which does that and a file to test your program on. Define a word as any sequence of \w characters. Note that the first and last word do NOT have to begin and/or end the line. There may be leading or trailing spaces or punctuation marks. These can be indicated by \W. Assume a line has at least 2 words.
So here is what I came up with for the assignment.
#!perl -w use strict; while (<>) { if (/^\W*(\w+)(\W+\w*\W*)+(\w+)\W+$/) { print "$1 $3\n"; } }
Sure, this does what the assignment asked, but I don't like it. I was hoping for some pointers on how to handle matching and ignoring the words and characters inbetween the first and last words.
Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.
|
---|
Replies are listed 'Best First'. | |
---|---|
(MeowChow) Re: Matching Words
by MeowChow (Vicar) on May 23, 2002 at 06:15 UTC | |
(jeffa) Re: Matching Words
by jeffa (Bishop) on May 23, 2002 at 06:26 UTC | |
Re: Matching Words
by Zaxo (Archbishop) on May 23, 2002 at 06:28 UTC | |
by tadman (Prior) on May 23, 2002 at 07:34 UTC | |
by MeowChow (Vicar) on May 23, 2002 at 07:47 UTC | |
by HollyKing (Pilgrim) on May 23, 2002 at 16:20 UTC |