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:

  1. Trim your question down to the essentials. Here that means leaving off details of what you are able to get working, especially if you find yourself noting that parts are relevant.
  2. Provide working--or at least compiling--code, by copy/pasting a fragement into <code></code> blocks rather than retyping. In the example above, <SIDX> became [SIDX], which is plainly not what you intended.
  3. Trim your examples down to the essentials that illustrate the problem. Here, the locking bits are just clutter.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.