Here using Data::Walk, Data::Visitor::Callback

#!/usr/bin/perl -- use strict; use warnings; use Data::Dumper (); Main( @ARGV ); exit( 0 ); sub Main { my $root = { 'foo' => { 'child1' => { 'gchild1' => { 'values' => { 'bar1' => 1, 'bar2' => 1 } } }, 'child2' => { 'gchild1' => { 'values' => { 'bar1' => 1, 'bar2' => 1 } }, 'gchild2' => { 'values' => { 'bar1' => 1, 'bar2' => 1 } } }, 'child3' => { 'values' => { 'bar1' => 1, 'bar2' => 1 } } } }; use Data::Visitor::Callback; my $vorp = sub { my @v; my $v = Data::Visitor::Callback->new( ignore_return_values => 1, hash_entry => sub { my ( $self, $k, $v ) = @_; push @v, $v if $k eq 'values' and $v; return; }, ); $v->visit( @_ ); return @v; }; print DD( $root, [ $vorp->( $root ) ], ), '#' x 33 , "\n"; use Data::Walk(); $vorp = sub { my @v; Data::Walk::walk( sub { ## same result as 2nd if block below #~ no warnings 'uninitialized'; #~ if( 'HASH' eq $Data::Walk::type and ref $_){ #~ my $v = $_->{values}; #~ push @v, $v if $v; #~ } if( $_ eq 'values'){ push @v, $Data::Walk::container->{values}; } return; }, @_ ); return @v; }; print DD( $root, [ $vorp->( $root ) ], ), '#' x 33 , "\n"; } sub DD { scalar Data::Dumper->new( \@_ )->Indent(1)->Useqq(1)->Dump; } __END__ $VAR1 = { "foo" => { "child2" => { "gchild2" => { "values" => { "bar1" => 1, "bar2" => 1 } }, "gchild1" => { "values" => { "bar1" => 1, "bar2" => 1 } } }, "child3" => { "values" => { "bar1" => 1, "bar2" => 1 } }, "child1" => { "gchild1" => { "values" => { "bar1" => 1, "bar2" => 1 } } } } }; $VAR2 = [ $VAR1->{"foo"}{"child2"}{"gchild2"}{"values"}, $VAR1->{"foo"}{"child2"}{"gchild1"}{"values"}, $VAR1->{"foo"}{"child3"}{"values"}, $VAR1->{"foo"}{"child1"}{"gchild1"}{"values"} ]; ################################# $VAR1 = { "foo" => { "child2" => { "gchild2" => { "values" => { "bar1" => 1, "bar2" => 1 } }, "gchild1" => { "values" => { "bar1" => 1, "bar2" => 1 } } }, "child3" => { "values" => { "bar1" => 1, "bar2" => 1 } }, "child1" => { "gchild1" => { "values" => { "bar1" => 1, "bar2" => 1 } } } } }; $VAR2 = [ $VAR1->{"foo"}{"child2"}{"gchild2"}{"values"}, $VAR1->{"foo"}{"child2"}{"gchild1"}{"values"}, $VAR1->{"foo"}{"child3"}{"values"}, $VAR1->{"foo"}{"child1"}{"gchild1"}{"values"} ]; #################################

Data::DPath might also be a candidate


In reply to Re: How to find all occurrences of a key in a deeply nested structure? by Anonymous Monk
in thread How to find all occurrences of a key in a deeply nested structure? by deMize

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.