On 11/16/10 Tue Nov 16, 2010 2:25 PM, "Vincent Li" <[email protected]> scribbled:
> On Tue, Nov 16, 2010 at 2:14 PM, Jim Gibson <[email protected]> wrote: >> On 11/16/10 Tue Nov 16, 2010 1:07 PM, "Vincent Li" >> <[email protected]> scribbled: >> >>> On Tue, Nov 16, 2010 at 9:38 AM, Jim Gibson <[email protected]> wrote: >> >>>> >>>> You need a parser to do this right. You might have some luck by reading the >>>> entire file into a scalar variable and using the Text::Balanced module >>>> together with some regular expressions to extract and parse the various >>>> blocks within the file. >>>> > Jim, thanks for the tips, I got it working now, but not with nested {} > block, I am running out of idea on the nested {} block issue, the > parser route seems complicate to me as my Perl is still rough :) Check out the Text::Balanced module. Using this module, you can extract nested, delimited blocks. Here is the process: 1. Read the file into a string (you have alread done this). 2. Loop: a. Use index to find the first '{' character. b. Remove and save the text before the '{'. c. Extract the {} block now at the beginning of your string using the extract_delimited function of Text::Balanced, saving the remaining text. d. See if the 'before' text matches one of your patterns. e. If it does, discard it and the extracted block. f. If it does not, write it out and the extract block. g. Repeat for the remainder text until you are out of text (step a. does not find a '{' character). Good luck. -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
