比较和编辑哈希中的基础结构 [英] Compare and edit underlying structure in hash

查看:83
本文介绍了比较和编辑哈希中的基础结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构复杂的哈希,我想执行搜索和替换.第一个哈希如下所示:

I have a hash of complex structure and I want to perform a search and replace. The first hash is like the following:

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

,我想迭代搜索所有'123'/'456'元素,如果找到匹配项,则需要对子层(即['ab','cd','ef']['as','sd','df'])进行比较,在这种情况下,仅保留带有['ab','cd','ef']的那个.因此输出如下:

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

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

因此,删除操作基于子结构,而不是索引.怎么做到呢?感谢您的帮助!

So the deletion is based on the substructure, and not index. How can it be done? Thanks for the help!!

让我们假设我将声明要保留的值,即我将保留456 => [" ab," cd," ef]基于预先声明的值[[ab","cd ," ef]并删除其他任何456实例.搜索必须针对每个键.因此,代码将遍历哈希,首先取123 => ["," yy," zy],然后将其与哈希的其余部分中的键进行比较,如果找不到匹配项,则不执行任何操作.如果找到匹配项,例如在456 => [" ab," cd," ef]的情况下,它将比较两者,并且正如我已经说过的那样,在匹配项中,带有["的匹配项ab," cd," ef]将被保留,它将保留456 => [" ab," cd," ef]并在哈希中其他任何地方丢弃456的任何其他实例,即它将在这种情况下,删除456 => ["as","sd","df"].

Lets assume that I will declare the values to be kept, i.e. I will keep 456 => ["ab", "cd", "ef"] based on a pre-declared value of ["ab", "cd", "ef"] and delete any other instance of 456 anywhere else. The search has to be for every key. so the code will go through the hash, first taking 123 => ["xx", "yy", "zy"] and compare it against the keys throughout the rest of the hash, if no match is found, do nothing. If a match is found, like in the case of 456 => ["ab", "cd", "ef"], it will compare the two, and as I have said that in case of a match the one with ["ab", "cd", "ef"] would be kept, it will keep 456 => ["ab", "cd", "ef"] and discard any other instances of 456 anywhere else in the hash, i.e. it will delete 456 => ["as", "sd", "df"] in this case.

推荐答案

以下是使用智能匹配运算符执行数组比较的解决方案:

Here is a solution that uses the smart match operator to perform the array comparison:

更新:正如Borodin所指出的,我的原始代码是错误的.这是固定版本.

Update: as Borodin pointed out, my original code was wrong. This is the fixed version.

更新2:对其进行了更改,以根据哈希结构选择要保留的值.

Update 2: Changed it to choose the values to keep based on a hash structure.

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};
        }
    }   
}

有关智能匹配运算符的更多信息,请参见 perlop .

For more on the smart match operator, see perlop.

这篇关于比较和编辑哈希中的基础结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆