First a few comments about the meta-problem: I had a rather difficult time understanding what it was you were asking for, in part because the title was misleading, in part because the problem statement included extraneous material that obscured the question, and a bit because the code fragment won't run.
To get good answers to your questions here, it really helps to:
As to your problem as I'm able to discern it from the code: You're walking two flat index files in parallel. For each pair of index lines, you attempt to find matches with as may keywords as possible, recording matches. The code shows that @resultline and @premium_results grow in parallel, with potential duplicates if multiple keywords match against either line. (I suspect this isn't what you want, and perhaps the nature of your index files prevents it.)
I'm guessing that you want to be doing something like the following, and I'm changing names to make the code clearer:
use strict; my(@results, @premium_results); open(RESULTS, "$data_dir/search1.idx") or die "$data_dir/search1.idx: $!"; while ( $line = <RESULTS> ) { foreach my $keyword ( @keywords ) { if ( $line =~ /\b$keyword\b/ ) { push @results, $line; last; } } close(RESULTS); open(PREMIUM, "$data_dir/search2.idx") or die "$data_dir/search2.idx: $!"; while ( $line = <PREMIUM> ) { foreach my $keyword ( @keywords ) { if ( $line =~ /\b$keyword\b/ ) { push @premium_results, $line; last; } } } close(PREMIUM);
This builds up @results and @premium_results independently, while ensuring that keywords match entire words (if this isn't what you want, remove the \b's), and ensuring that a line is only recorded once if any keyword matches. Nothing in the code you showed suggests that the two index files need to be walked in parallel. And if the first index is smaller than the second, you risk false negatives on the extra, unread lines in the second index.
A simple refactoring--left as an exercise--is to take the two largely common code block and make them a subroutine that takes a filename as input and returns an array of matching lines as output.
In reply to Re: Assigning scalars to scalar template includes
by dws
in thread Assigning scalars to scalar template includes
by Dente
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |