in reply to Matching Words
How about this?
The pop and shift are needed because split leaves empty elements at the ends if nonword characters are there.#!perl -w use strict; while (<>) { my @words = split /\W+/; pop @words if $words[-1] eq ''; # pop @words unless $words[-1]; shift @words if $words[0] eq ''; # shift @words unless $words[0]; print $words[0], ' ', $words[-1], $/; }
A couple of comments on your code. I think you mean \W*$/ at the right of your regex. Also, you can use a (?:\W+\w*\W*)+ to get grouping without capture.
Update: I sure like MeowChow's array slice in print:
print "@words[0,-1]$/";
Update 2: A palpable hit, ++tadman. Corrected.
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Matching Words
by tadman (Prior) on May 23, 2002 at 07:34 UTC | |
by MeowChow (Vicar) on May 23, 2002 at 07:47 UTC | |
|
Re: Re: Matching Words
by HollyKing (Pilgrim) on May 23, 2002 at 16:20 UTC |