[英]How can I open a javascript object in Perl?
I have a JavaScript Object file with 5.5mb of longitude latitude data and I would like to open it in Perl so I can apply a crude detail reducing algorithm that would then save a new object file with the reduced dataset. 我有一个包含5.5mb经度纬度数据的JavaScript对象文件,我想在Perl中打开它,因此我可以应用粗略的细节缩减算法,然后将其与缩减的数据集保存在一起。 For reducing detail I use a for loop that only takes every 20th long/lat pair.
为了减少细节,我使用了一个for循环,该循环只需要每20个长/纬线对。
I can do this in javascript but this requires that I copy/paste each coordinate set and run my JavasSript on it one at a time. 我可以使用javascript做到这一点,但这需要我复制/粘贴每个坐标集,并一次在其上运行JavasSript。
I then thought perhaps I could take each set of coordinates and put them in to a SQL db but that seems like a crude way to do it. 然后,我想也许我可以获取每组坐标并将其放入SQL数据库中,但这似乎是一种粗略的方法。 And moves a lot of data around.
并移动大量数据。
I settled on Perl being one of the better options, to do it all on the server. 我认为Perl是更好的选择之一,可以在服务器上完成所有这些工作。
I can open the file with: 我可以使用以下方式打开文件:
#!/usr/bin/perl
# open file
open(FILE, "reduced_object_latlng.js") or die("Unable to open file");
# read file into an array
@data = <FILE>;
# close file
close(FILE);
# print file contents
foreach $line (@data)
{
print $line;
}
The object follows this design: 该对象遵循以下设计:
var paths = {
mayo: {
name: 'Mayo',
colour: 'green',
coordinates: '-9.854892,53.76898 -9.853634,53.769338 -9.85282,53.769387 -9.851981,53.769561 -9.850952,53.769508 -9.850129,53.769371 -9.849136,53.769171 **data**'
},
galway: {
name: 'Galway',
colour: 'purple',
coordinates: '**data**;
}
}; //etc.
To illustrate how I reduce the above data my javascript version loads from a file with one var coords = "*data*"
为了说明如何减少上述数据,我的javascript版本从一个
var coords = "*data*"
的文件中加载了我的javascript版本
coords = coords.split(" ");
var path = [];
var output="";
document.getElementById("map_canvas").innerHTML = "";
for (var i = 0; i < coords.length; i++) {
if (i%20==0)
{
var coord = coords[i].split(",");
output += coord[0]+","+coord[1]+" ";
}
}
document.getElementById("map_canvas").innerHTML = output;
I have read some suggesting I convert it to JSON, I'm not sure if I need to do that. 我读过一些建议,建议将其转换为JSON,但不确定是否需要这样做。 And instead of writing a pure text handler is there a way to load the file as an object?
而不是编写纯文本处理程序,有没有办法将文件作为对象加载?
I was stuck for time so I did it this way: 我被时间困住了,所以我这样做是这样的:
var outputobject = 'var paths = {';
for (property in copypaths) {
outputobject += property + ': { ';
outputobject += "name: '" + copypaths[property].name+"',";
outputobject += "colour: '"+ copypaths[property].colour+"',";
var reducedoutput="";
var coord = copypaths[property].coordinates.split(" ");
for (var i = 0; i < coord.length; i++) {
if (i%20==0)
{
var coords = coord[i].split(",");
reducedoutput += coords[0]+","+coords[1]+" ";
}
}
outputobject += "coordinates: '"+ reducedoutput+"'},";
}
outputobject += "};";
document.getElementById("reduced").innerHTML = outputobject;
it still involves copy/paste and deleting the last ,
. 它仍然涉及复制/粘贴和删除最后
,
。 Thank you @Oleg V. Volkov, when I have more time later in the week I'll look at the method you laid out. 谢谢@Oleg V. Volkov,当我在本周晚些时候有更多时间时,我将介绍您提出的方法。
只需剥离领先的JavaScript,即可使用裸键获得几乎正确的JSON,并使用将allow_barekey
设置为true值的JSON
/ JSON::PP
实例来解码生成的字符串。
Using JSON
is your best bet. 最好使用
JSON
。 It has options that allow you to decode less stringent JSON syntax, and you will need 它具有允许您解码不太严格的JSON语法的选项,并且您将需要
allow_singlequote
to allow single-quoted as well as double-quoted strings allow_singlequote
允许单引号和双引号字符串
allow_barekey
to allow all-alphanumeric hash keys to have no quotes at all allow_barekey
允许所有字母数字哈希键完全没有引号
decode_prefix
to ignore junk after the end of the data 数据结束后,
decode_prefix
会忽略垃圾
relaxed
for good luck relaxed
好运
The program below decodes the JSON to a Perl structure, extracts the coordinates string for the mayo
entry, and prints the values out in pairs. 下面的程序将JSON解码为Perl结构,提取
mayo
条目的坐标字符串,并成对输出这些值。
Note that I have removed the semicolon and added a quote at the end of coordinates: '**data**;
请注意,我删除了分号并在
coordinates: '**data**;
末尾添加了引号coordinates: '**data**;
as I assume this is a mistake rather than actual JavaScript data 因为我认为这是一个错误,而不是实际的JavaScript数据
use strict;
use warnings;
use JSON -support_by_pp;
my $json = JSON->new->relaxed->allow_singlequote->allow_barekey;
my $data = do {
local $/;
<DATA>;
};
my ($hash) = $json->decode_prefix($data =~ /(\{.*)/s);
my @coords = $hash->{mayo}{coordinates} =~ /[-0-9.]+/g;
printf "%f %f\n", splice @coords, 0, 2 while @coords;
__DATA__
var paths = {
mayo: {
name: 'Mayo',
colour: 'green',
coordinates: '-9.854892,53.76898 -9.853634,53.769338 -9.85282,53.769387 -9.851981,53.769561 -9.850952,53.769508 -9.850129,53.769371 -9.849136,53.769171 **data**'
},
galway: {
name: 'Galway',
colour: 'purple',
coordinates: '**data**'
}
}; //etc.
output 产量
-9.854892 53.768980
-9.853634 53.769338
-9.852820 53.769387
-9.851981 53.769561
-9.850952 53.769508
-9.850129 53.769371
-9.849136 53.769171
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.