Compare and edit the base structure in a hash

I have a hash of complex structure and I want to do a search and replace. The first hash is as follows:

$VAR1 = {
  abc => { 123 => ["xx", "yy", "zy"], 456 => ["ab", "cd", "ef"] },
  def => { 659 => ["wx", "yg", "kl"], 456 => ["as", "sd", "df"] },
  mno => { 987 => ["lk", "dm", "sd"] },
}

and I want to iteratively search for all elements of '123' / '456', and if a match is found, I need to do a sublayer comparison, i.e. ['ab','cd','ef']and ['as','sd','df'], in this case, keep only one with ['ab', 'cd', 'ef']. Thus, the output will look like this:

$VAR1 = {
  abc => { 123 => ["xx", "yy", "zy"], 456 => ["ab", "cd", "ef"] },
  def => { 659 => ["wx", "yg", "kl"] },
  mno => { 987 => ["lk", "dm", "sd"] },
}

Thus, deletion is based on a substructure, not an index. How can I do that? Thanks for the help!

, , , .. 456 = > [ "ab" , "cd", "ef" ] [ "ab" , "cd", "ef" ] 456 . . , 123 = > [ "xx", "yy", "zy" ] , , . , 456 = > [ "ab" , "cd", "ef" ], , , , [ ab "," cd ", "ef" ], 456 = > [" ab "," cd ", "ef" ] 456 , delete 456 = > ["as "," sd "," df"] .

0
1

, smart match :

:, , . .

2: , .

my $VAR1 = {
  abc => { 123 => ["xx", "yy", "zy"], 456 => ["ab", "cd", "ef"] },
  def => { 659 => ["wx", "yg", "kl"], 456 => ["as", "sd", "df"] },
  mno => { 987 => ["lk", "dm", "sd"] },
};

my %keep_values = (
    '456' => ['ab','cd','ef']
);

foreach my $outer_key (keys %$VAR1)
{
    foreach my $keepers (keys %keep_values)
    {
        if (exists $VAR1->{$outer_key}{$keepers} and 
            #use the smart match operator to compare arrays.
            !(@{$VAR1->{$outer_key}{$keepers}} ~~ @{$keep_values{$keepers}}))
        {
            delete $VAR1->{$outer_key}{$keepers};
        }
    }   
}

smart match . perlop.

0

All Articles