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

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

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
    I don't think that's quicker, in terms of run time.

    Quicksilver wants to do it for every word in the file, which means that using a regex to search for it will scale like O(nē) (n being the length of the input file) in time, while the traversal of the files is possible in O(n).

      You're right. I completely misread the question. split seems a likely candidate:

      #! perl -slw use strict; while( <DATA> ) { my @words = split /[^a-zA-Z']+/; for ( 0 .. $#words ) { printf "%s, $., %s %s %s\n", $words[ $_ ], $_ ? $words[ $_ -1 ] : '', $words[ $_ ], $_ < $#words ? $words[ $_ + 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

      Outputs

      [14:37:07.78]C:\test>junk4 little, 1, little boy boy, 1, little boy blue blue, 1, boy blue come come, 1, blue come blow blow, 1, come blow on on, 1, blow on your your, 1, on your horn horn, 1, your horn give, 2, give a a, 2, give a little little, 2, a little take take, 2, little take a a, 2, take a lot lot, 2, a lot give, 3, give a a, 3, give a lot lot, 3, a lot take take, 3, lot take a a, 3, take a little little, 3, a little Mary, 4, Mary had had, 4, Mary had a a, 4, had a little little, 4, a little lamb lamb, 4, little lamb Don't, 5, Don't belittle belittle, 5, Don't belittle the the, 5, belittle the little little, 5, the little guy guy, 5, little guy

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.