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

Hi monks

i ve a problem in executing loop,i want to close the body tag before the reference or notes,or acknowledgement,if three statement will not present , the loop should close the body tag at the of chapter

the below code close the tag but the problem is , if the three statement present at a in a chapter mean it should close the body tag which statement come first.

The three statement may come in any order,my code put the tag before the reference statement because i put reference at the first position of the loop

References|Note|Acknowledgement,Note|References|Acknowledgement
if($line =~ /<P_sec\d>(?:<\w>)?(<xref ref-type="page" id="page_\d+"\/> +)?(?:<\w>)?(References|Note|Acknowledgement)(?:<\/\w>)?<\/P_sec\d>/<\ +/body>\n$&/si) { } else{ $line .= "</body>\n"; }

pls give me a suggestion to solve my problem,thanks

Replies are listed 'Best First'.
Re: detection in Loop condition
by Marshall (Canon) on Jan 21, 2012 at 06:26 UTC
    You are running a substitution regex (a very complex one at that) $line =~ s/.../MISSING/ upon $line, but I don't see a substitution in this regex.

    You need to simplify the problem - the code that you have makes no sense.

Re: detection in Loop condition
by Anonymous Monk on Jan 21, 2012 at 04:42 UTC

      sorry..i ve nearly 30 chapters in a book and it is XML file and i'm getting one by one chapter for closing the body tag

      the tag should close before reference|notes|ack,these statement may come in any order,in a chapter, which statement come first,the tag should close before that

        Do you mean your input file is an XML-file, but with the <body> tag not closed or closed at a wrong place? If the <body> tag is not closed then your XML-file is invalid and you better remedy that first.

        If you just want to move the <body> tag to some other place (which means you want to re-order your XML-tree structure) then consider that XML-files are best handled with one of the many XML-parsers and not with regexes.

        If you can show a (small / stripped down, but still relevant) example of your XML-file, you will get a much better reply from the Monks here.

        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

        So instead of providing one line of input, that represents your real input, you'd rather attempt to describe it using English?

        So what we , random strangers, can interpret your English the way you do, provided you didn't omit anything, and then construct a $line correctly? Both how it looks before the change you want to make, and after?

        So that we can then craft a regular expression, that you can use to make the substitution you're attempting?

        But you yourself said you're not an native English speaker, so we'd be guessing the whole time.

        You've managed to provide input in all these previous postings of yours XML tag closing, XML tag closing, string concatenation, split function using multiple delimiters

        Do you want help or not?