I'm trying to neaten up a large data structure in Perl which was read in from JSON. Two stereotypical elements look like this (in JSON):
[
[ [ {'payload':'test'} ], [ [ {'payload':'reply'} ], [] ] ],
[ [ {'payload':'another thread'} ]
]
I want to completely remove that empty arrayref at the bottom of that element, and replace each arrayref containing only a single hashref by the contained hashref. In other words, the result should be this:
[
[ {'payload':'test'}, [ {'payload':'reply'} ] ],
[ {'payload':'another thread'} ]
]
Currently my code is as follows:
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'));
and though it succeeds in removing the empty arrayref, it fails to collapse the singletons. How can this code be corrected such that it can collapse the singletons?