How to split this array in perl?

How to split the next array so that I can get lon and lat points only every time? In perl script?

[{"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}]

Any help?

+5
source share
4 answers

I would also recommend using the JSON library, but if this is just one thing, you can do something like this:

#!/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}]

Result:

$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'
      ]
    ];
+1
source

You can use the JSON library in Perl to parse your JSON string. After that, you will have an array of hashes that you can iterate over and extract the lat / lon values.

+7
source

JSON, j0nes, , lat lon, :

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 , .
+5

As already noted, using a library for this task would be much better (see my sample code below). If you do it like flesk, you have to make sure that it latalways appears after lon. This might be a little more reliable, but note that I am sharing data in chunks that are not valid JSONstring:

#!/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}]

Conclusion:

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

A better and simpler solution would simply use the JSON library :

#!/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}]

Exit: the same as above.

So, please do not do this manually if you do not know what you are doing! :)

+2
source

All Articles