他の人がすでに述べたように、このタスクにはライブラリを使用する方がはるかに優れています (以下のコード例を参照)。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}]
出力: 上と同じ。
ですから、自分が何をしているのか完全に理解していない限り、手作業で行わないでください! :)