|Syntactic Confectionery Delight|
RFC: Is there a better way to use Text::Balanced?by Lady_Aleena (Curate)
|on Nov 13, 2014 at 03:49 UTC||Need Help??|
Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:
Hello. I have been writing a function to help me parse my lines. I had gotten help on this previously, however, I could not get it to work in Perl 5.8.8. I gave Text::Balanced a try and got it to work on my system. (I haven't uploaded the new code to my web host yet. I am trying to manage my expectations.) Before I get too excited about it, I ask for someone to look it over and let me know where I may encounter problems, if any. Have a cookie.
Any of those could have attributes, so I denote them like...
And they can be nested like...
Here is the string I ran through my new inline function for the examples. I know it is long, but I will be parsing longer strings through inline.
The first thing I wanted to do was make sure any inline HTML comments I put in just for me are stripped. So I remove those first with s/\s\<!.+$// on line 15. Now the party can really start by extracting the brackets on line 17.
Since my lines can have more than one set of brackets in them, I have to use extract_multiple in conjunction with extract_bracketed. So, using those together, I get the following array.
I was looking at it, and in moments I figured out how to group the results. It appears every other value is what is the bracket found with the value before it being what comes before the bracket. The last value is what appears after the last bracket, if anything. To make it a little easier for me to munge the lines, I need to group them. So I wrote a little function called fancy_splice to group the lines together.
So, after putting the array returned by extract_multiple through fancy_splice, I get...
Now the party is in full swing with my lines with partners with the exception of the last guy standing alone at the end in this case. Because he is such a party pooper, I am going to deal with him first in lines 17 to 23 by assigning his value to $end. I pop off his group from the rest and send him to the bar.
Now I turn on the music and get the pairs dancing on line 25. The first partner I deal with is the lead, $bracket->. On his tails is the tag which goes with the contents of the bracket (the last word of the value). So, I assign everything else in the lead to $start and put the last word into $tag.
Now, we will have to skip two loops ahead here to explain why $tag also needs munging. If you look at the first group's second value, you will see EurSUP. Well EurSUP is not an inline HTML tag, and I just want the SUP. So, lines 30 through 36 deal with getting the tags I want and rejoining the part, which is not the tag, with $start.
Now I am done dealing with the lead, it is time to deal with his partner which takes a bit more work. The first things I need to do are to name her $tagged and take her brackets off (line 38). I next look to see if the tag has any attributes, so I split $tagged by the pipes, in any. I then check the last value of the array has a > in it, and if not, pop it off the array and assign it to the $attribute. (If it did have a > in it, then it needs to be kept with the $in_tag because it may be the attribute of the next level tag or even the level after next.
When the dance is done for on set, I put the pieces together on line 43 and concatenate that piece to $line. When all the sets are done dancing, I bring $end back from the bar, and concatenate him too. All the dancing produces...
Thank you for taking the time to read this, have another cookie. Please let me know if you see a problem or a place where I can tighten things up.
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!