【问题标题】:Read An array from a file into a perl array [duplicate]将文件中的数组读入perl数组[重复]
【发布时间】:2014-03-15 13:51:21
【问题描述】:

我有一个如下所示的文件:

{
"status": "success",
"msg": {
    "status": "success",
    "inscount": 2,
    "critical": 0,
    "result": [
        {
            "Insulin": "Insuman Rapid",
            "morning change": 0,
            "noon change": 1,
            "evening change": 0,
            "action": 3,
            "change morning from": "22",
            "change noon from": "9",
            "change evening from": "20",
            "change morning to": "22",
            "change noon to": "12",
            "change evening to": "20",
            "change type": "1"
        },
        {
            "Insulin": "Insuman basal",
            "morning change": 0,
            "noon change": 0,
            "evening change": 0,
            "action": null,
            "change morning from": "7",
            "change noon from": "6",
            "change evening from": "8",
            "change morning to": "7",
            "change noon to": "6",
            "change evening to": "8",
            "change type": “1”
        }
    ],
    "balance": "9974"
}

}

这是来自 Web 服务的 JSON 响应,我将其保存到临时文件中。文件。 我想将结果数组提取到一个 perl 对象数组中。

它是通过以下服务调用生成的

system("curl -X POST -H '$CONTENT_TYPE'  -d '$ARGS'  -o  $TEMP_FILE  $SERVICE_URL 2>/dev/null");

我正在使用这段代码来提取状态、关键和计数

    if (open(OUTFILE,"<$TEMP_FILE")) {
    while(<OUTFILE>) {
        chomp;
        if (/status\"?\:\s*\"success\"/) {
            $SUCCESS=1;
            print"Success File open********* Febin :) \n";
        }

        if (/critical\"?\:\s*\"1\"/) {
            $CRITICAL=1;
        }

        if (/change type\"?\:\s*\"(\d+)\"/) {
            $CHANGE_TYPE=$1;
        }

        if (/balance\"?\:\s*\"([^\"]+)\"/) {
            $BALANCE=$1;
        }

        foreach $key (keys %TIME_VALUES) {
            if(/$key\schange\"?\:\s*\"1\"/) {
                $TIME_VALUES{$key}[0] = 1;
            }

            if(/change $key from\"?\:\s*\"([^\"]+)\"/) {
                $TIME_VALUES{$key}[1] = $1;
            }

            if(/change $key to\"?\:\s*\"([^\"]+)\"/) {
                $TIME_VALUES{$key}[2] = $1;
            }
        }
    }
    close(OUTFILE);

请问有什么方法可以解决

我是 perl 脚本的新手

【问题讨论】:

  • 另外,当您可以使用 LWP + CPAN 中的 JSON 解析模块时,为什么需要保存到临时文件?
  • @john jensen。因为我正在编辑一个现有的星号 IVR 代码,它已经由其他人完成。随着服务的变化,Json 响应将在此之前以数组的形式发送结果

标签: arrays json perl file


【解决方案1】:

也许不完全理解这个问题,但恕我直言,以下方法可以工作:

use Modern::Perl;
use JSON::XS;
use Encode;
use File::Slurp;
use Data::Dumper;

my $file = "./data.json";
my $data = decode_json Encode::encode 'utf8', read_file $file, { binmode => ':utf8' } ;

my $res;
$res->{$_} = $data->{msg}->{$_} for( qw(status critical inscount));

say Dumper $res;

为您的输入文件生成:

$VAR1 = {
          'critical' => 0,
          'inscount' => 2,
          'status' => 'success'
        };

您应该使用 JSON::XS 将 JSON 解析为内部 perl 结构,如果您的数据只是 ascii,则可以省略 encode/utf 部分...

编辑 - 提高可读性 - 评论:

use Modern::Perl;  #use some modern perl features, like: say and strict and warnings

                   #"load" some modules:
use JSON::XS;      #for the JSON parsing
use Encode;        #module for encoding from/to different encodings
use File::Slurp;   #module for reading files into a variable
use Data::Dumper;  #module for dumping data structures

my $file = "./data.json";  #the filename, where your "JSON" data is

#read the file content into the variable (read_file - provided by the File::SLurp)
my $json_from_file = read_file $file, { binmode => ':utf8' };

#encode
my $encoded_json = Encode::encode 'utf8', $json_from_file;

#convert JSON to internal perl-data structure
my $perl_data = decode_json $encoded_json;

say "=== the data structure ===";
say Dumper $perl_data;

# copy the needed data
my $status   = $perl_data->{msg}->{status};
my $critical = $perl_data->{msg}->{critical};
my $inscount = $perl_data->{msg}->{inscount};
#this is useless, because you can use the $perl_data->{msg}->{status} directly

【讨论】:

  • 但我想从文件中读取这个
  • 这正是他正在做的事情
  • @FebinFathah 补充说明
  • 这段代码对我有用
猜你喜欢
  • 2011-08-22
  • 2012-10-28
  • 2012-07-08
  • 2017-06-07
  • 2020-06-14
  • 2023-03-30
  • 1970-01-01
  • 2010-12-25
  • 2018-07-11
相关资源
最近更新 更多