Generally your program would have been better written as a Finite-state_machine, further examples here and here. Basically you read each line and decide in a big switch or if-then-else construct, what the next state should be. That is something you are already doing a little with $bset and $bend, but then you try to read and parse 4 lines in one go and it gets complicated and you have to write a lot of code more than once.

A state machine helps to prevent this. You have only one place where you read a line from the file. Then depending on state and line you do something and change the state

In your case there would be one state "outside define service" and one state "inside define service" (you can use a number to denote state but you should document what that number means). If you are in state "inside define service" there are three cases:

1) the line begins with "service_description": You don't print the line but push the line into an array. Set a flag that tells you you have seen it

2) the line begins with some other text: You don't print the line but push the line into an array.

3) the line is a '}': look for the 'use' line in the array and remove the sam if the flag is not set. Print the array and set the state to "outside define service".

In the "outside define service" state you just look for a "define service" line and if you find one, change state and clear the array and flag

The program would look something like this:

... my $state="outside"; my @servicelines; my $desc_flag; #is 1 if description found while ($line = <TEXT_FILE>) { if ($state eq "outside") { print MYFILE $line; if ($line=~/^\s*define service/) { $state="inside"; @servicelines=(); $desc_flag=0; } } else if ($state eq "inside") { if ($line=~/^\s*}/) { @servicelines= removesam(@servicelines) if ($desc_flag); print @servicelines; print $line; $state="outside"; } elsif ($line=~/^\s*service_description/) { push @servicelines, $line; $desc_flag=1; } else { push @servicelines, $line; } } } ...

Missing from above is the sub removesam, also any error checks, to warn you if the file you are parsing is not conforming to expectations. For example a '}' line in state "outside" should print a warning. Also as a general advice you should add "use warnings;" and "use strict;" at the start of your program even though strict mode is slightly more work for you, it prevents errors.


In reply to Re^3: How to parse a text file by jethro
in thread How to parse a text file by xoroz

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.