Monks,

I have a KML file that I need to parse. I would like to do this without the help of a module. Here is an example:
<Placemark> <name>This is the title</name> ... <coordinates>12,34,0</coordinates> </Placemark> <Placemark> <name>This is another title</name> </Placemark> <Placemark> <name>One more</name> <coordinates>56,78,0</coordinates> ... <coordinates>-99,-88,0</coordinates> </Placemark>
I would like to create hashes from all occurrences of <name> and its corresponding <coordinates>.
foreach (@kmlFile) { if (m/<name>(.*)<\/name>/) { %{$1} = (name => "<name>$1</name"); } }
Great, all I need is a way to find any <coordinates> tags that occur on lines after <name>, so that each coordinate is stored in a hash with its corresponding name. One of the problems with this is that there are some <name> tags that exist without <coordinates> (center three lines in the example above).

In other words, the code need only extract the next <coordinates> that occur after a <name> -- these coordinates would only be associated with the <name> that preceeds it, all other <coordinates> would be ignored until found again after the next <name>.

These occurrences are not necessarily on consecutive lines

From the above, I would need:
%This is the title = ( name => "<name>This is the title</name", coordinates => "<coordinates>12,34,0</coordinates>", ) %One more = ( name => "<name>One more</name>", coordinates => "<coordinates>56,78,0</coordinates>", )

If a mod is the only option, please let me know...

Thanks for any help,

-Newbs


In reply to RegEx Riddle by ferddle

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.