[EMAIL PROTECTED] wrote:
$word = "seminary";


I am trying to retrieve a word, and the surrounding text in a file. For example, read through a file for $word, and when matched, place in a variable the matching word, along with the 10 words preceding it and trailing it, and have it work over multiple lines. Is there an effective way to do this in Perl? i.e., in the paragraph above, if I searched for the word "along" match would contain what is written below. Can anyone help me with this? Thanks.


$match = "and when matched, place in a variable the matching word, along with the 10 words preceding it and trailing it, and";


There is most likely a regex that can handle this in a very nice and neat fashion (but I haven't started taht book yet ;-)). I would approach it from a different angle...


Slurping all of the text you want to search into a single scalar, then just use a simple 'split' on that text using the word you want to get the text around. Then each time it occurs in the text will give you the words before and after into an array. Then you can step through the elements of the array to get the # of words you want before and after each instance of the word that has been split on. This could be accomplished with a regex, or an additional split.

Give it a shot, post some code and ask some more questions if you get stuck....

perldoc -f split

As a hint if you go the regex method you might also want to check out the '\b' special character.

http://danconia.org


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to