Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: aligning textby roboticus (Chancellor) |
on Feb 02, 2018 at 17:07 UTC ( [id://1208334]=note: print w/replies, xml ) | Need Help?? |
Anonymous Monk: No-one else seems to have mentioned the perils of parsing XML with regular expressions, so I guess I'll do so. It's all fine so long as the XML continues to come in to you formatted as your example, or if you control both ends of the data feed. However, when dealing with third-party data feeds, at some point, something will eventually happen and they'll change the formatting to give you a headache. For example, suppose the data comes in like this:
Here, you'll find several things that can cause you some trouble:
So you'll find that you'll get awful results with your code:
Notice that due to the ugliness I added to the XML file, the output is not only ugly, but wrong! Not only are some items missing from the output, but since you're using separate arrays to keep your values, any parsing error one one of the values makes your arrays get out of synchronization, so the wrong prices appear on some items. There are other headaches you can get into when dealing with XML files, too. So you may want to learn one of the XML handling libraries. It's a little bit of a pain at first, but once you're used to it, these sorts of issues just magically go away. Then you can use the time you're not wrestling XML data to handle the other issues, like formatting values! I used XML::Twig and whipped something up and it displays:
...roboticus When your only tool is a regular expression, all XML problems look insurmountable.
In Section
Seekers of Perl Wisdom
|
|