in reply to Finding word either side of a word match
It might be a bit quicker using a regex:
#! perl -slw use strict; my $word = 'little'; while( <DATA> ) { m[( [^ \n]*[^\w\n]* (\b\Q$word\E\b) [^\w\n]*[^ \n]* )]x and print "$2, $., $1"; } __DATA__ little boy blue come blow on your horn give a little, take a lot give a lot, take a little Mary had a little lamb Don't belittle the little guy
Gives:
[14:04:33.30]C:\test>junk4 little, 1, little boy little, 2, a little, take little, 3, a little little, 4, a little lamb little, 5, the little guy
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding word either side of a word match
by moritz (Cardinal) on Mar 03, 2008 at 14:21 UTC | |
by BrowserUk (Patriarch) on Mar 03, 2008 at 14:44 UTC |