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


In reply to RegExp Loop by ramigi

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.