in reply to How to parse a text file
And if jbts excellent advice didn't help you, then yes, posting of the code is welcome and even suggested in How do I post a question effectively?. Especially when your code is ugly, monks can do a lot for code beauty ;-).
But please don't use pre-tags, use <p>-tags for text and <code>-tags for code and data.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to parse a text file
by xoroz (Initiate) on Aug 10, 2009 at 14:02 UTC | |
I checked the Nagios::Object::Config; but I dont think its for me, since I just need to parse a .cfg file and I am not even in the nagios server... If I just could find an element "{" inside the array mark its index find the end element "}" then split its values... someone must have done simillar to these, I have searched for hours about arrays in perl but still no good...
| [reply] [d/l] |
by jethro (Monsignor) on Aug 10, 2009 at 16:47 UTC | |
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:
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. | [reply] [d/l] |
by xoroz (Initiate) on Aug 11, 2009 at 10:59 UTC | |
Jethro, you are the man.
| [reply] [d/l] |
by ww (Archbishop) on Aug 10, 2009 at 14:58 UTC | |
Hey! Now that you're listening, please EDIT both your parent and grandparent nodes, replacing <pre>...</pre> tags with <p>...</p> and <c>...</c> tags. Your present markup borks the display for most Monks. As you were told by message, if you need more on appropriate markup here, see Markup in the Monastery. | [reply] |