读取文本文件并将信息保存到perl中的数组数组中

时间:2013-04-09 10:57:01

标签: perl perl-data-structures

我有一个包含以下代码的文本文件。我想创建perl代码来读取该包含并在perl中形成相应的数据结构。我可以使用下面的perl代码阅读 .txt 文件,但是如何在perl数据结构中读取和保存所有内容,如Arrays of arrays/Hashes of arrays/Arrays of hashes/Hashes of hashes,任何人都可以帮助我吗?

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

上面我写了代码:

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);

1 个答案:

答案 0 :(得分:3)

输入看起来像JSON。使用适当的模块:

use JSON;
my $s = from_json(join q(), <>);