[OSM-dev] Split osm line with perl

Anthony osm at inbox.org
Sun Nov 29 16:31:41 GMT 2009


On Sun, Nov 29, 2009 at 6:16 AM, Maarten Deen <mdeen at xs4all.nl> wrote:
> Does anyone have an idea (or is there already a routine) how to split a line of
> an osm file in its respective keys and values?

On Sun, Nov 29, 2009 at 6:16 AM, Maarten Deen <mdeen at xs4all.nl> wrote:
> Does anyone have an idea (or is there already a routine) how to split a line of
> an osm file in its respective keys and values?

I ultimately chose to use C for my parser, so I'm not sure if this
actually works or not, but here was my attempt at it:

my @parts=split(' ',$4);
my $numparts=scalar(@parts);
for (my $count=0; $count<$numparts; $count++) {
  my $part=$parts[$count];
  my ($k, $v)=split('=', $part, 2);
   while ($v!~/^".*"$/) {
      $count++;
      die("count is $count; part is $part; $line") if ($count>=$numparts);
      $part.=" ".$parts[$count];
      ($k, $v)=split('=', $part);
    }
    $v=XML::Entities::decode('all',$v) if ($v=~/\&/);

> I've tried a few things, but I'm not fluent in perl. My problem at the moment is
> that splitting a line on the space character seems logical, but you run into
> problems if a value has a space in it.
> So splitting something like <tag k="name" v="foo bar"/> will split the value
> "foo bar" also.

That's handled above by the while loop.

Let me know if it works.  I haven't touched that code in a while!  :)




More information about the dev mailing list