tags:

views:

59

answers:

4
$str = "Data = [ {"name": "test","Address": "UK" "currency": "£" },{"name": "test2","Address": "US" "currency": "$" },{"name": "test","Address": "eur" "currency": "E" }

I want to display all address

its not multi line string . It all a single string

Please help on this

Thanks , TREE J

+3  A: 

Your string is JSON! Treat it as such!

edit: I'm an idiot and can't tell when a question is tagged as perl instead of PHP :-) Link ammended.

fredley
Correction, bar the `Data = ` at the beginning. Where did you get the data from?
fredley
+2  A: 

This should work:

while ($str =~ /\"Address\":\S+\"(.*?)\"/g) {
      print "Address = $1\n";
}
ennuikiller
no, the g modifier makes certain to get all matches!!
ennuikiller
sorry my mistake !!!!
Tree
A: 

something like:

my $str = q(Data = [ {"name": "test","Address": "UK" "currency": "£" },{"name": "test2","Address": "US" "currency": "$" },{"name": "test","Address": "eur" "currency": "E" });
my @addresses = $str =~ /"Address":\s*"([^"]*)"/g;
print "@addresses\n";

HTH,

Paul

(PS: post real code, not pseudo code...)

pavel
+2  A: 

You do it by using the right tool for the job. In this case you fix the corrupt JSON with a regex and then use JSON to get the data:

#!/usr/bin/perl

use strict;
use warnings;

use JSON;

my $input  = <DATA>;
my ($json) = $input =~ /DATA = (.*)/;
my $data   = decode_json $json;

for my $record (@$data) {
    print "$record->{name} has address $record->{Address}\n";
}

__DATA__
DATA = [ {"name": "test",  "Address": "UK", "currency": "£" }, {"name": "test2", "Address": "US", "currency": "$" }, {"name": "test",  "Address": "eur", "currency": "E" } ]
Chas. Owens