It seems to me that the problem is not that you want to detect both strings, but that you want to print them in the order of precedence, even if that's not the order in which they appear in the file. So you already know what order you want; you want to see "trap" first, and "hair" second. But they appear in the file in such order that "hair" comes before "trap".

This means you should not print immediately upon finding a match. Instead, you should accumulate your matches, and then print them in the order of relevance or importance.

Is that what you're after? If so, let us know.

Update: Here's one way you can do that. We maintain a list of triggers in some order of precedence. We also construct a list of matches. Then we run through the matches in trigger-list-order, printing them out. If there are two lines that trip the same trigger, they will be printed in the order they were seen, but otherwise we maintain trigger order rather than file order. I think that's what you were looking for:

use strict; use warnings; my @order = qw( trap hair ); my %found; my $search = join '|', @order; while( <DATA> ) { if( m/($search)/i ) { chomp; push @{$found{lc $1}}, $_; } } foreach my $wanted ( @order ) { if( exists $found{$wanted} ) { print "$_\n" for @{$found{$wanted}}; } } __DATA__ Transformers robots in disguise I whip my hair back and forth I will catch a dog with a trap

The output:

I will catch a dog with a trap I whip my hair back and forth

As you can see, the output comes in the order that you set in @order. If there had been two lines that match the same trigger, however, those lines would maintain file-order. So first priority goes to trigger order, and then within trigger order, file order takes second priority.


Dave


In reply to Re^3: Multiple strings in a file by davido
in thread Multiple strings in a file by The_Last_Monk

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.