At this point (with the code as shown just now), you're only getting one match because every time you find a match, you completely reset the value of "$results{$number}" -- you would want something like this whenever a match is found:
$results{$number} .= "$url... @before <B>$1</B> @after <BR/>";
Note the concatenation operator ".=" Sorry -- I wasn't paying close-enough attention. If you're saying that a given $term might occur more than once on a given line, and you're only getting the first occurrence, not both, yeah, that makes sense. You do a next after processing the first match of $term on each line. The logic I suggested in my update about doing KWIC searches will fix this. Otherwise, you have to do something like:
for $term ( @$inputs ) {
while ( /\b$term\b/g ) {
...
}
}
BTW, please note the update I made in my earlier reply, about doing KWIC. I think other replies in this thread have explained about seeking to the beginning of the file, which is now a moot point. no longer relevant.
Another update, to answer your question about references: you're right, the input args are not being changed, but I'm suggesting that you pass two arrays to the sub: one is a list of files to search in, and the other is a list of terms to search for; using references to arrays allows you to pass both of these in one sub call -- if you don't use array refs, you're just passing an undifferentiated list, and the sub has no way of knowing where one array ends and the other begins. (whew! sorry about the mess!)
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.