我正在尝试整理从 JSON 读取的 Perl 中的大型数据结构。两个典型的元素看起来像这样(在 JSON 中):
[
[ [ {'payload':'test'} ], [ [ {'payload':'reply'} ], [] ] ],
[ [ {'payload':'another thread'} ]
]
我想完全删除该元素底部的空 arrayref,并将每个仅包含单个 hashref 的 arrayref 替换为包含的 hashref。换句话说,结果应该是这样的:
[
[ {'payload':'test'}, [ {'payload':'reply'} ] ],
[ {'payload':'another thread'} ]
]
目前我的代码如下:
use v5.12;
use strict;
use warnings;
use JSON::XS;
use Data::Walk;
sub cleanup {
if (ref $_ eq 'ARRAY') {
if (scalar(@{$_}) == 0) {
die 'mysteriously I never reach this branch!';
while (my ($key,$value) = each @{$Data::Walk::container}) {
if ($value == $_) {
delete ${$Data::Walk::container}[$key]
}
}
} elsif (scalar(@{$_}) == 1 and ref @{$_}[0]) {
$_ = @{$_}[0];
} else {
my $tail = ${$_}[scalar(@{$_})-1];
if (ref $tail eq 'ARRAY' and scalar(@{$tail}) == 0) {
$#{$_}--;
}
}
}
}
sub get {
my $begin = shift;
$begin = 0 unless $begin;
my $end = shift();
$end = $begin + 25 unless $end;
my $threads;
{
local $/;
open(my $f, '<emails.json');
$threads = decode_json <$f>;
close($f);
}
$threads = [ @{$threads}[$begin .. $end] ];
walkdepth(\&eliminate_singleton, $threads);
return $threads;
}
print JSON::XS->new->ascii->pretty->encode(&get('subject:joke'));
尽管它成功地删除了空数组引用,但它未能折叠单例。如何更正此代码以使其可以折叠单例?