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

Dear All,
My program should match an element in a text, and print the sentence with a title preceding it (all sentences have titles). However in the loop I created, the matching of the element I'm looking for is completely ignored and it prints all the lines (it idetifies the title only). Where have I gone wrong??

Thanks,
Rami

#!/usr/local/bin/perl use strict; use warnings; ########################################################### #In this program there is a text always ontaining a title #followed by + # #a sentence.I wish to check if there is a regexp B in the #actual sent +ence. # #If so, I wish to print the title of this sentence and the #sentence i +tself # #If the element B does not appear in the sentence, I want #both the se +ntence # #and its title to be #ignored. + # ###################################################################### +####### my $text = "A This is a title of the first sentence some text here containing the element B I need and more text here A This is a title of the second sentence C some text here but no element I need so it should be ignored A This is a title for the third sentence last sentence with the element B that I'm looking for"; my $expa = "A"; #title element which appears before every #sentence my $expb = "B"; #the regexp I'm loooking for my $line; # the line my $s; while($line = $text){ if($line =~m/$expa/){ #if A is found $s = $line; #then, read the following line $line = $text; if($line =~m/$expb/){ #if B is found print "$s\n\t$line\n\n\n"; #print both lines } } }

20041002 Janitored by Corion: Added code tags and formatting

Replies are listed 'Best First'.
Re: RegExp Loop
by Eimi Metamorphoumai (Deacon) on Oct 02, 2004 at 12:55 UTC
    Your problem is that you're putting all the text into $text and then making your while loop while($line = $text). But since $text is just a simple variable, the while loop will only execute once, with $line equal to the whole text (that is, all the lines). Then inside the loop, you're using $line as if it will magically get the next line, instead of keeping the same value. How best to proceed depends on how you're getting your data into the program. The easiest way would be if you're reading it from standard in, or from a file (or, as in the sample code I'll show here, from the special DATA filehandle), because then you can loop over it. Otherwise, you'll want to split it into an array of lines first with split "\n", $text.
    #!/usr/bin/perl use strict; use warnings; my $expa = "A"; #title element which appears before every #sentence my $expb = "B"; #the regexp I'm loooking for my $s; while (<DATA>){ chomp; if (/$expa/){ $s = $_; chomp($_ = <DATA>); if (/$expb/){ print "$s\n\t$_\n\n\n"; } } } __DATA__ A This is a title of the first sentence some text here containing the element B I need and more text here A This is a title of the second sentence C some text here but no element I need so it should be ignored A This is a title for the third sentence last sentence with the element B that I'm looking for
    I'm trying to keep your logic mostly the same, though I'm not sure I entirely understand it. If you're reading from a file, substitute the filehandle for DATA (or if you want the default, you can just use <>.
Re: RegExp Loop
by ccn (Vicar) on Oct 02, 2004 at 12:41 UTC

    print "$expa\n\t$_\n\n\n" for grep /$expb/, split /$expa/, $text;