The_Last_Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I am having trouble matching more than 2 strings across multiple lines in a text file. So here is what i have
open my $text, "<$textFile>" or die "Dead"; while(<$text>){ if(/$firstString/m){ print $_; } if(/$secondString/m){ print $_; } }
This however, only works if the first string is on the first line and second string is on the second line. What would be the best for it to search the entire file for both those strings, and if those strings show up anywhere within the text file, print the line they are in? With regards, A novice monk

Replies are listed 'Best First'.
Re: Multiple strings in a file
by toolic (Bishop) on Feb 10, 2015 at 17:41 UTC
    use warnings; use strict; while (<DATA>) { print if /thing1/ or /thing2/; } __DATA__ thing1 foo bar thing2 thing1 and thing2

    prints:

    thing1 thing2 thing1 and thing2
      Tried that, didn't work. Ok so lets say my text file has the following sentences
      Transformers robots in disguise I whip my hair back and forth I will catch a dog with a trap
      Now lets say, ignoring the case, the first word i want to look for is Trap, and the second word i want to look for is Hair. Basically it should print
      I will catch a dog with a trap i whip my hair back and forth
      Is that more clear? Sorry for not making it clear before.

        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

Re: Multiple strings in a file
by LanX (Saint) on Feb 10, 2015 at 17:37 UTC
    > matching more than 2 strings across multiple lines

    I'm not sure which question you are asking, but think you are looking for the flip-flop operator

    try

    open my $text, "<", $textFile>" or die "Dead"; while(<$text>){ if(/$firstString/ .. /$secondString/){ print $_; } }

    otherwise please show desired input and output.

    Cheers Rolf

    PS: Je suis Charlie!

      Ok so lets say my text file has the following sentences
      Transformers robots in disguise I whip my hair back and forth I will catch a dog with a trap
      Now lets say, ignoring the case, the first word i want to look for is Trap, and the second word i want to look for is Hair. Basically it should print
      I will catch a dog with a trap i whip my hair back and forth
      Is that more clear? Sorry for not making it clear before.
        > Is that more clear?

        no because your code should already do this, if you don't want double matches follow toolic's advice.

        Cheers Rolf

        PS: Je suis Charlie!

Re: Multiple strings in a file
by hotpelmen (Scribe) on Feb 10, 2015 at 17:56 UTC
    Your code seems to work if you remove closing > when you open the file.
    use strict; use warnings; my ($textFile, $firstString, $secondString) = @ARGV; open my $text, "<$textFile" or die "Dead"; while(<$text>){ if(/$firstString/m){ print $_; } if(/$secondString/m){ print $_; } }
    When I pass it its own filename it gives
    $ perl foo.pl foo.pl first second my ($textFile, $firstString, $secondString) = @ARGV; my ($textFile, $firstString, $secondString) = @ARGV; if(/$firstString/m){ if(/$secondString/m){
      I'm sorry, i accidentely put the closing brace. its not there, and unfortunately, doesn't work for me.
Re: Multiple strings in a file
by CountZero (Bishop) on Feb 10, 2015 at 20:02 UTC
    I think this will do what you want (as per davido's suggestion):
    use Modern::Perl qw /2014/; use Data::Dump qw /dump/; my @searchstrings = qw/trap hair dog I/; my @found; while (<DATA>) { while ( my ( $index, $search ) = each @searchstrings ) { push @{ $found[$index] }, [ $search, $., $_ ] if /$search/; } } say dump(@found); __DATA__ Transformers robots in disguise I whip my hair back and forth I will catch a dog with a trap
    Output:
    ( [["trap", 3, "I will catch a dog with a trap\n"]], [["hair", 2, "I whip my hair back and forth\n"]], [["dog", 3, "I will catch a dog with a trap\n"]], [ ["I", 2, "I whip my hair back and forth\n"], ["I", 3, "I will catch a dog with a trap\n"], ], )

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Multiple strings in a file
by Discipulus (Canon) on Feb 11, 2015 at 08:18 UTC
    Only a note: seems weird the way you open files:
    open my $text, "<$textFile>" or die "Dead";
    should be:
    open my $text, '<', $textFile or die "Dead because: $!";
    Id est the 3 args form of open. You can find more info in the docs for open.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.