Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl grep nested hashes recursive

Tags:

hash

nested

perl

I have structure which looks like this (hash of hashes):

%hash=(
Level1_1=> {    
 Level2_1 => "val1",
 Level2_2=> { 
  Level3_1 => "val2",
  Level3_2 => "val1",
  Level3_3 => "val3",
 },
Level2_3 => "val3",
},
 Level1_2=> {   
  Level2_1 => "val1",
  Level2_2=> {  
   Level3_1 => "val1",
   Level3_2 => "val2",
   Level3_3 => "val3",
  },
 Level2_3 => "val3",
 },
 Level1_3=> {   
  Level2_1 => "val1",
  Level2_2 => "val2",
  Level2_3 => "val3",
 });

I would like to grep this nested structure filtered by "val2" And the output should be :

%result=(
    Level1_1=> { Level2_2=> { Level3_1 => "val2"} },
    Level1_2=> { Level2_2=> { Level3_2 => "val2" } },
    Level1_3=> { Level2_2 => "val2" }
    );

My first idea was to use a recursive subroutine like this:

hashwalk_v( \%hash );
sub hashwalk_v
{
    my ($element, @array) = @_;
    if( ref($element) =~ /HASH/ )
    {
   while (my ($key, $value) = each %$element)
   {

     if( ref($value) =~ /HASH/ ) {
      push (@array, $key);
      hashwalk_v($value, @array);
     } else {
      if ( $value =~ "val2") {
       push (@array, $key);
       print $_ .  "\n" for @array;
      } else {
       @array =""; 
      }
     }
   }
 }
}

but unfortunately I can not save the hash key from the previous loop. Any ideas??

like image 860
ovntatar Avatar asked Oct 28 '13 07:10

ovntatar


2 Answers

Similar approach,

use Data::Dumper; print Dumper hfilter(\%hash, "val2");

sub hfilter {
  my ($h, $find) = @_;
  return if ref $h ne "HASH";

  my %ret = map {
    my $v = $h->{$_};
    my $new = ref($v) && hfilter($v, $find);

    $new ? ($_ => $new)
      : $v eq $find ? ($_ => $v)
      : ();

  } keys %$h;

  return %ret ? \%ret : ();
}
like image 103
mpapec Avatar answered Nov 14 '22 21:11

mpapec


Recursion is the right answer, but you seem to have gotten a little confused along the way. :) Build the new hash as you go, one level at a time:

sub deep_hash_grep {
    my ($hash, $needle) = @_;

    my $ret = {};
    while (my ($key, $value) = each %{$hash}) {
        if (ref $value eq 'HASH') {
            my $subgrep = deep_hash_grep($value, $needle);
            if (%{$subgrep}) {
                $ret->{$key} = $subgrep;
            }
        }
        elsif ($value =~ $needle) {
            $ret->{$key} = $value;
        }
    }

    return $ret;
}
like image 29
Eevee Avatar answered Nov 14 '22 22:11

Eevee