5

毎回経度と緯度のポイントのみを取得できるように、次の配列を分割する方法は? perl スクリプトで ?

[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.804376,"lat":12.991606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.894376,"lat":12.999606,"bfg":18000,"xyz":null,"jky":null}]

助けはありますか?

4

4 に答える 4

7

Perlの JSON ライブラリを使用して、JSON 文字列を解析できます。その後、反復して緯度/経度の値を抽出できるハッシュの配列が得られます。

于 2012-09-10T08:21:28.457 に答える
6

j0nesJSONで提案されているようにライブラリを使用します。これは、印刷して標準で提供されるワンライナーです。latlon

perl -MJSON -le '$, = "\t"; map { print $_->{lon}, $_->{lat} } @{JSON->new->decode(<>)}'

出力:

77.594376   12.971606
77.604376   12.980606
77.674376   12.981606
77.684376   12.982606
77.744376   12.983606
77.784376   12.990606
77.804376   12.991606
77.824376   12.995606
77.874376   12.997606
77.894376   12.999606

説明:

  • -MJSONパッケージをロードします。
  • $,フィールド区切り文字をタブに設定します。
  • mapデコード関数によって返された配列参照内の各ハッシュに print コマンドをマップします。
于 2012-09-10T09:37:14.300 に答える
2

他の人がすでに述べたように、このタスクにはライブラリを使用する方がはるかに優れています (以下のコード例を参照)。flesk のように行う場合は、latが常に の後に来ることを確認する必要がありlonます。これはもう少し堅牢ですが、データを有効なJSON文字列ではないチャンクに分割していることに注意してください。

#!/usr/bin/env perl

use strict;
use warnings;

# split in (invalid) object parts
my @chunks = split /},/ => <DATA>;

# iterate over chunks
for (@chunks) {

    # extract latitude/longitude
    my $lon = /"lon":([^,]+)/ ? $1 : 'UNKNOWN';
    my $lat = /"lat":([^,]+)/ ? $1 : 'UNKNOWN';

    # print data
    print "lon: $lon, lat: $lat\n";
}

__DATA__
[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.804376,"lat":12.991606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.894376,"lat":12.999606,"bfg":18000,"xyz":null,"jky":null}]

出力:

lon: 77.594376, lat: 12.971606
lon: 77.604376, lat: 12.980606
lon: 77.674376, lat: 12.981606
lon: 77.684376, lat: 12.982606
lon: 77.744376, lat: 12.983606
lon: 77.784376, lat: 12.990606
lon: 77.804376, lat: 12.991606
lon: 77.824376, lat: 12.995606
lon: 77.874376, lat: 12.997606
lon: 77.894376, lat: 12.999606

はるかに優れたシンプルなソリューションは、JSONライブラリを使用するだけです。

#!/usr/bin/env perl

use strict;
use warnings;
use JSON 'decode_json';

# decode input
my $objects = decode_json <DATA>;

# iterate over objects and print data
printf "lon: %f, lat: %f\n", $_->{lon}, $_->{lat} for @$objects;

__DATA__
[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.804376,"lat":12.991606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.894376,"lat":12.999606,"bfg":18000,"xyz":null,"jky":null}]

出力: 上と同じ。

ですから、自分が何をしているのか完全に理解していない限り、手作業で行わないでください! :)

于 2012-09-10T09:25:54.447 に答える
1

JSONライブラリも使用することをお勧めしますが、それが1つだけの場合は、次のようにすることができます。

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my @data;

while (<DATA>) {
    if (/"lon":([^,]+),"lat":([^,]+)/) {
        push @data, [$1, $2];
    }
}

print Dumper \@data;

__DATA__
[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},"lon":77.804376,"lat":12.991606,bfg":18000,"xyz":null,"jky":null},"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jk":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},"lon":77.894376,"lat":12999606,"bfg":18000,"xyz":null,"jky":null}]

結果:

$VAR1 = [
      [
        '77.594376',
        '12.971606'
      ],
      [
        '77.674376',
        '12.981606'
      ],
      [
        '77.744376',
        '12.983606'
      ],
      [
        '77.804376',
        '12.991606'
      ],
      [
        '77.874376',
        '12.997606'
      ],
      [
        '77.894376',
        '12.999606'
      ]
    ];
于 2012-09-10T09:12:10.203 に答える