Thank you. I am trying to add the rejected HoH to the XML as new nodes in the else part of the statement but I am getting error in forming the new XML with the rejected HoH.
my $myhash = \%HoH;
my $dom = 'XML::LibXML'->load_xml(location => 'test.osm');
for my $node ($dom->findnodes('/osm/node')) {
my $tag = $node->findnodes('tag[@k="name"]')->[0];
my $key = "$node->{lat}.$node->{lon}";
if (exists $HoH{$key}){
$tag->{v} = $HoH{$key}->{name};
$node->{id} = $HoH{$key}->{id};
}
else {
print "$_\n" for keys %HoH;
my($lat_key, $lon_key) = split(/./, $, 2);
my $new_node = 'XML::LibXML::Element'->new("node");
my $id = 'XML::LibXML::Attr'->new("id",$HoH{$key}->{id});
my $lat = 'XML::LibXML::Attr'->new("lat",$lat_key);
my $lon = 'XML::LibXML::Attr'->new("lon",$lon_key);
$new_node->setAttribute($id);
$new_node->setAttribute($lat);
$new_node->setAttribute($lon);
my $new_node_child = 'XML::LibXML::Element'->new("tag");
my $k = 'XML::LibXML::Attr'->new("k",'name');
my $v = 'XML::LibXML::Attr'->new("v",$HoH{$key}->{name});
$new_node_child->setAttribute($k);
$new_node_child->setAttribute($v);
$new_node->addChild($new_node_child);
}
}
$dom->toFile('new_test.osm');
| [reply] [d/l] |
my($lat_key, $lon_key) = split(/./, $, 2);
The first argument to split is a regular expression in which the dot is a metacharacter and should be escaped. Note that since both lat and lon also contain a dot perhaps some other separator character would be a better choice if you want to split the keys. A comma would do and would not need to be escaped.
but I am getting error
You've chosen not to share the precise text of the error message. I cannot imagine why.
| [reply] [d/l] |
Sorry, It was my mistake, I pasted the wrong code. The correct code is below. I am trying to add new nodes in the XML with the rejected HoH keys but during building, I am getting an error of "bad name at /usr/local/lib/x86_64-linux-gnu/perl/5.30.0/XML/LibXML.pm".
When I am splitting the key to lat long, it gets split with the number, not the decimal part.
In the else part, how can I get the rejected HoH keys?
Thank you.
my $dom = 'XML::LibXML'->load_xml(location => 'test.osm');
for my $node ($dom->findnodes('/osm/node')) {
my $tag = $node->findnodes('tag[@k="name"]')->[0];
my $key = "$node->{lat}.$node->{lon}";
if (exists $HoH{$key}){
$tag->{v} = $HoH{$key}->{name};
$node->{id} = $HoH{$key}->{id};
}
else {
foreach my $key (keys %HoH) {
#print $key,"\n";
my ($lat_key,$lon_key) = (split /[.]/, $key)[0,2];
my $new_node = 'XML::LibXML::Element'->new("node");
my $id = 'XML::LibXML::Attr'->new("id",$HoH{$key}->{id});
my $lat = 'XML::LibXML::Attr'->new("lat",$lat_key);
my $lon = 'XML::LibXML::Attr'->new("lon",$lon_key);
$new_node->setAttribute($id);
$new_node->setAttribute($lat);
$new_node->setAttribute($lon);
my $new_node_child = 'XML::LibXML::Element'->new("tag");
my $k = 'XML::LibXML::Attr'->new("k",'name');
my $v = 'XML::LibXML::Attr'->new("v",$HoH{$key}->{name});
$new_node_child->setAttribute($k);
$new_node_child->setAttribute($v);
$new_node->addChild($new_node_child);
}
}
}
$dom->toFile('new_test.osm');
| [reply] [d/l] |
my($lat_key, $lon_key) = split(/./, $, 2);
In addition to hippo's comment, note that the
quoted extract from this does not compile.
While there is a $, Perl special variable (see
perlvar), you don't seem to be using it
(nor would I recommend doing so in this application). I suspect this is
just a cut/paste error and not the error your code is actually
producing, but again, who knows?
Win8 Strawberry 5.8.9.5 (32) Mon 01/11/2021 18:12:15
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings
$, = 'foo.bar';
my($lat_key, $lon_key) = split(/\./, $, 2);
Number found where operator expected at - line 3, near "$, 2"
(Missing operator before 2?)
^Z
syntax error at - line 3, near "$, 2"
Execution of - aborted due to compilation errors.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
| [reply] |