How to split this array in perl?

How to split the following 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 ?

Answers


I'd recommend using a JSON library as well, but if it's just a one off, you could 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'
      ]
    ];

You can use a 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.


Use the JSON library as suggested by j0nes, here's a one-liner that prints lat and lon given on standard in:

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

Output:

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

Explanation:

  • -MJSON loads the package.
  • $, sets the field delimiter to tab.
  • map maps the print command onto each hash in the array reference returned by the decode function.

As others already stated, using a library for this task would be much better (see my code example below). If you do it like flesk, you have to make sure, the lat comes always after the lon. A little bit more robust could be this, but note I'm splitting the data in chunks that aren't valid JSON strings:

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

Output:

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 far better and simpler solution would just 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}]

Output: the same as above.

So, please don't do it by hand unless you're absolutely knowing what you're doing! :)


Need Your Help

javascript ajax on submit

javascript ajax google-maps

i am using a google map api to get location. i have used the below function onchange of address field. it is working properly onchange. i have used same function on submit in the same form to check...

what's the Risk of “Ad Hoc Distributed Queries”

sql-server

I use an users to login SQLServer(not sa)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.