Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How to find all occurrences of a key in a deeply nested structure?

by Anonymous Monk
on Oct 20, 2011 at 15:13 UTC ( [id://932702]=note: print w/replies, xml ) Need Help??


in reply to How to find all occurrences of a key in a deeply nested structure?

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

  • Comment on Re: How to find all occurrences of a key in a deeply nested structure?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to find all occurrences of a key in a deeply nested structure?
by deMize (Monk) on Oct 20, 2011 at 15:35 UTC
    Very helpful. I was on my way of doing the same thing, but didn't see the needed container. If I were to try and get a unique list of all the values, does this make sense:
    my $struct = {...}; # the hash of hashes in the question my $found; Data::Walk::walk \&wanted, $struct; print join(" ",sort keys %$found) ,"\n"; sub wanted{ if (/^values$/){ foreach ( keys %{$Data::Walk::container->{$_}} ){ $found->{$_}=1; } } }



    Demize
      No, don't do that, do it the way I did it :) except only push if not $Data::Walk::seen
        But that would return duplicates and not a unique set, which is why I was storing in a hash, to do the cleanup for me :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://932702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found