我用过:print Dumper $decoded
,$decoded
HASH变量在哪里,我把它作为输出.
$VAR1 = { 'Case_345' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' }, 'Case_512' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' }, 'Case_534' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' }, 'Case_552' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' }, 'Case_578' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' }, 'Case_466' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' }, 'Case_754' => { 'Notes' => 'test2', 'Submit_Date' => '2015-11-21 00:53:22 UTC', 'Last_Resolved_Date' => '' } };
这是我尝试使用输出一些值来测试迭代HASH的内容.
foreach my $key ( keys %decoded ) { print "key: $key, value: $decoded{$key}\n"; }
它不会通过$decoded
变量进行迭代.我是perl的新手,有人可以帮助我.如果它有助于我通过转换字符串得到这个JSON::XS::decode_json($json_str)
.谢谢.
第一件事是它$decoded
是一个包含哈希引用的标量变量,与哈希变量无关.要访问引用的哈希,必须取消引用它%decoded
$decoded
%$decoded
您必须始终 use strict
并且use warnings 'all'
在每个 Perl程序的顶部,特别是在向其他人寻求帮助之前.在这种情况下,这是一个简单的衡量标准,可以捕捉到你在%decoded
没有声明的情况下使用过的事实
另外,值的哈希值都比较哈希引用,并打印出来会产生一种不到这样有用
key: Case_466, value: HASH(0xbf4840) key: Case_534, value: HASH(0xc6dc00) key: Case_552, value: HASH(0x280fce0) key: Case_512, value: HASH(0xbf4930) key: Case_345, value: HASH(0xd4c2f0) key: Case_754, value: HASH(0x280fdd0) key: Case_578, value: HASH(0x280fd58)
大概你想要每个哈希中所有字段的值?要在给定键和对应写入的哈希的引用的情况下访问哈希元素的值$hashref->{key}
,因此此代码将为您转储整个哈希
for my $key ( keys %$decoded ) { my $data = $decoded->{$key}; print "key: $key, values:\n"; for my $field ( qw/ Submit_Date Last_Resolved_Date Notes / ) { printf " %-18s => %s\n", $field, $data->{$field}; } }
key: Case_578, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2
key: Case_754, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2
key: Case_534, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2
key: Case_345, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2
key: Case_512, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2
key: Case_552, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2
key: Case_466, values:
Submit_Date => 2015-11-21 00:53:22 UTC
Last_Resolved_Date =>
Notes => test2