For a given key:

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111952 use warnings; use List::Util qw( none ); use Data::Dump qw( dd ); sub subtree { my ($key, $tree) = @_; if( ref $tree eq 'ARRAY' ) { my @items = map [ subtree( $key, $_ ) ], @$tree; return +(none { $_->[0] } @items ) ? ( 0, []) : ( 1, [ map { $_->[0] ? $_->[1] : undef } @items ] ); } elsif( ref $tree eq 'HASH' ) { my @wanted; for my $k ( sort keys %$tree ) { if( $k eq $key ) { push @wanted, [ 1, $k => $tree->{$k} ] } else { my ( $w, $t ) = subtree( $key, $tree->{$k} ); $w and push @wanted, [ $w, $k => $t ]; } } return @wanted > 0, +{ map { @{ $_ }[1, 2] } @wanted }; } else { return 0, $tree } } sub pathforkey { my ($key, $tree, $sofar) = @_; if( ref $tree eq 'HASH' ) { return map { $key eq $_ ? $sofar . "{$_}" : (), pathforkey( $key, $tree->{$_}, $sofar . "{$_}" ) } sort keys %$tree; } elsif( ref $tree eq 'ARRAY' ) { return map { pathforkey( $key, $tree->[$_], $sofar . "[$_]" ) } 0 .. $#$tree; } else { return () } } my $tree = gettree(); dd 'original tree', $tree; print "\n"; my ($want, $subtree) = subtree( 'field', $tree ); dd "the subtree for 'field' ", $subtree; print "\nall paths to key 'field' :\n"; print "$_\n" for pathforkey( 'field', $tree, '' ); sub gettree # slightly reduced... { my $tree = { 'cookies' => { '_AVESTA_ENVIRONMENT' => 'prod', '_mcid' => '1.340667ccb3eb6552450356561ab6bd92.' }, 'error' => '400 Bad Request', 'content' => { 'detail' => 'The resource specific details, \'errors\' array.', 'errors' => [ { 'message' => 'This value of type object.', 'field' => 'merge_fields' } ], 'status' => 400, 'title' => 'Invalid Resource', 'type' => 'http://developer.mailchimp.', 'instance' => '59edf1fc-ed93-4a40-ba23-1f0af24a6eb3' }, 'code' => '400' }; }

Outputs:

( "original tree", { code => 400, content => { detail => "The resource specific details, 'errors' +array.", errors => [ { field => "merge_fields", message => " +This value of type object." }, ], instance => "59edf1fc-ed93-4a40-ba23-1f0af24a6eb3", status => 400, title => "Invalid Resource", type => "http://developer.mailchimp.", }, cookies => { _AVESTA_ENVIRONMENT => "prod", _mcid => "1.340667ccb3eb6552450356561ab6bd92.", }, error => "400 Bad Request", }, ) ( "the subtree for 'field' ", { content => { errors => [{ field => "merge_fields" }] } }, ) all paths to key 'field' : {content}{errors}[0]{field}

In reply to Re: Dump filtered subtree? by tybalt89
in thread Dump filtered subtree? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.