Rework hash vs array derreference

This commit is contained in:
Alberto Simões
2023-03-11 16:15:06 +00:00
parent 513039dd53
commit 1ac6bb9050
3 changed files with 34 additions and 17 deletions

View File

@@ -196,21 +196,30 @@ sub _traverse {
foreach my $key (keys %{$reference}) {
if (ref($reference->{$key}) eq 'ARRAY') {
# just use the 1st one, more elements in array are expected to be the same
foreach my $item (@{$hash->{$key}}) {
if (ref($item) eq q(HASH)) {
# traverse the structure pushing our key to the @tree
$self->_traverse($reference->{$key}->[0], $item, @tree, $key);
}
else {
# a value, this is tricky
$self->_traverse(
{ item => $reference->{$key}->[0] },
{ item => $item },
@tree, $key
);
# either it is undefined (optional values)
# or it should be an array, so we can derreference it.
if (!defined($hash->{$key}) || ref($hash->{$key}) eq "ARRAY") {
# just use the 1st one, more elements in array are expected to be the same
foreach my $item (@{$hash->{$key}}) {
if (ref($item) eq q(HASH)) {
# traverse the structure pushing our key to the @tree
$self->_traverse($reference->{$key}->[0], $item, @tree, $key);
}
else {
# a value, this is tricky
$self->_traverse(
{ item => $reference->{$key}->[0] },
{ item => $item },
@tree, $key
);
}
}
}
else {
push @{$self->{errors}}, "$key is not an array";
}
}
elsif (ref($reference->{$key}) eq 'HASH') {
$self->_traverse($reference->{$key}, $hash->{$key}, @tree, $key);