It seems from your questions that you are building something like a search engine. Maybe
How to build a Search Engine. is of interest for you (a very good node IMHO), and maybe
this article on perl.com.
If you insist on creating your index manually, here is what you could do: in the while loop create a list of all found words, and then iterate over all of them again:
# assuming you stored all matches in @words
my %word_context;
for (1 .. $#words-1){
push @{$word_context{$words[$_]}, [$words[$_ - 1], $words[$_ + 1]];
}
Though it might be better to store context and line number in the same data structure.
You also have to think about the first and the last word, which are special in that they don't have two words of context each. What do you want to do with them?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.