ferddle has asked for the wisdom of the Perl Monks concerning the following question:
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:I would like to create hashes from all occurrences of <name> and its corresponding <coordinates>.<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>
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).foreach (@kmlFile) { if (m/<name>(.*)<\/name>/) { %{$1} = (name => "<name>$1</name"); } }
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RegEx Riddle
by moritz (Cardinal) on May 28, 2008 at 06:44 UTC | |
by pc88mxer (Vicar) on May 28, 2008 at 15:13 UTC | |
|
Re: RegEx Riddle
by prasadbabu (Prior) on May 28, 2008 at 05:47 UTC | |
by reasonablekeith (Deacon) on May 28, 2008 at 10:12 UTC | |
|
Re: RegEx Riddle
by punch_card_don (Curate) on May 28, 2008 at 18:28 UTC |