sub traverse_hash { my ($jsonhash, $found, @keys_to_find) = @_; my $results = 'results.txt'; open(FILE, ">>$results"); for my $key (sort keys %{$jsonhash}) { my $keyvalue = $jsonhash->{$key}; if ('HASH' eq ref $keyvalue) { $found = {%$found, %{ traverse_hash($keyvalue, $found, @keys_to_find) }}; } for my $find (@keys_to_find) { if ($key eq $find) { if ('HASH' eq ref $keyvalue) { $found->{$key} = [ map $keyvalue->{$_}, sort keys %$keyvalue ]; } else { $found->{$key} = $keyvalue; } } } } if (@keys_to_find == keys %$found) { my $count; $count = @{ (grep 'ARRAY' eq ref $_, values %$found)[0] // [] }; $count ||= 1; for my $i (0 .. $count - 1) { print FILE join(':', map { 'ARRAY' eq ref $_ ? $_->[$i] : $_ } @{$found}{@keys_to_find}), "\n"; } $found = {}; } return $found; close(FILE); }