0

I have a text file which contains following code. I want create perl code to read that containt and form a corresponding data structure in perl. I can read the .txt file with below perl code but how to read and save that all in perl data structur like Arrays of arrays/Hashes of arrays/Arrays of hashes/Hashes of hashes , anybody can help me?

{
   "Availability" : {
      "142" : {
         "141" : "1.042",
         "161" : "0.000",
         "162" : "0.000"
      }
   },
   "Average Jitter to Destination" : {
      "142" : {
         "141" : "192.309",
         "161" : "13.323",
         "162" : "37.003"
      }
   },
   "Average Jitter to Source" : {
      "142" : {
         "141" : "192.309",
         "161" : "13.323",
         "162" : "37.003"
      }
   },
   "Average Round Trip Time" : {
      "142" : {
         "141" : "8557.511",
         "161" : "772.278",
         "162" : "389.566"
      }
   },
   "Packet Loss to Destination" : {
      "142" : {
         "141" : "0.000",
         "161" : "0.000",
         "162" : "0.000"
      }
   },
   "Packet Loss to Source" : {
      "142" : {
         "141" : "0.000",
         "161" : "0.000",
         "162" : "0.000"
      }
   }

For above I wrote code:

print "content-type: text/html \n\n"; #HTTP HEADER
$dirname = "/ravikiran/html/JSONData.txt";
$dirpath = "$ENV{DOCUMENT_ROOT}$dirname";
sysopen(HANDLE, $dirpath , O_RDWR);
my %data;`enter code here`
while($line = <HANDLE>) {
chomp $line;
print $line;
}
close (HANDLE);
tostao
  • 2,803
  • 4
  • 38
  • 61
ravikiran
  • 105
  • 1
  • 1
  • 7

1 Answers1

3

The input looks like JSON. Use the appropriate module:

use JSON;
my $s = from_json(join q(), <>);
choroba
  • 231,213
  • 25
  • 204
  • 289