首页 文章

Perl如何构建动态多级哈希查找

提问于
浏览
0

我有一个代码块,我使用了很多次轻微变化,我试图进入子程序 . 此代码块完成配置模板(路由器接口,vrf,其他网络内容)

它通过在通过摄取excel文件构建的散列数据结构(称为%config_hash)中查找数据来实现:P . 查找的数据位于不同模板的散列的不同区域 .

当前工作代码的一个例子是:

my @temp_source_template = @{ clone ($source_template{$switch_int_template}) };

            my %regex_replacements=();                                                  ## hash for holding regex search and replace values, keys are !name! (look in template files) values taken from DCAP
            my @regex_key =();                                                          ## temp array used for whe more then one !name! on a line
            my $find_string='';
            foreach my $line (@temp_source_template){
                my (@regex_key) = ( $line =~ /(\!.*?\!)/g );                        ## match needs to be non greedy thus .*? not .*
                foreach my $hash_refs (@regex_key){
                    my $lookup = $hash_refs =~ s/!//gri;                            ## remove ! from !name! so lookup can be done in DCAP file hash
                    my $excel_lookup = $lookup =~ s/_/ /gri;
                    $regex_replacements{$hash_refs} = $config_hash{'Vlan'}{$inner}{$excel_lookup};          ## lookup DCAP file hash a write value to regex hash
                    if (undef eq $regex_replacements{$hash_refs}){
                        $regex_replacements{$hash_refs} = $config_hash{'Switch'}{$outer}{$excel_lookup};
                    }
                    if (undef eq $regex_replacements{$hash_refs}){
                        $regex_replacements{$hash_refs} = $config_hash{'VRF'}{$middle}{$excel_lookup};
                    }
                    $find_string= $find_string . $hash_refs . '|' ;

                }

            }

因此,这会创建一个散列(regex_replacements),其中包含要查找的值(regex_replacements中的散列键)和值,以替换那些(regex_replacements中的值) . 它还构建了一个在regex表达式中使用的字符串($ find_string) . 不同的模板将具有不同的哈希查找“路径”(例如$ config_hash {'Switch'} {$ outer} {$ excel_lookup})或不同的顺序(实际上是最具体的匹配)

为了完整性,这里是执行正则表达式替换的代码块:

foreach my $line (@temp_source_template){
    my (@line_array) = split /(![A-Za-z_]*!)/, $line;
    foreach my $chunk (@line_array){
        my $temp_chunk = $chunk;
        $chunk =~ s/($find_string)/$regex_replacements{$1}/gi;
        if (!($chunk)){
            $chunk = $temp_chunk;
        }
    }
    $line = join ("", @line_array);

    if ($line =~ /\!.*\!/){
        print {$log} " ERROR line has unmatched variables deleting line \"$line\"\n";
        $line ="";
    }
}

所以我做了一些搜索,我发现了这个:Perl: How to turn array into nested hash keys这几乎正是我想要的但我无法让它工作,因为我的变量引用是一个哈希,它的哈希变量引用只是"REF"所以我得到错误试图使用哈希作为参考 .

所以我不会发布我尝试过的内容,因为我并不真正了解该链接的魔力 .

但我正在做的是传递给下面的子

my @temp_source_template = @{ clone ($source_template{$test}) };
        my @search_array = ( ['VRF' ,$middle] , ['Switch' ,$outer]);
        my $find_string, $completed_template = dynamic_regex_replace_fine_string_gen(\%source_config,\@temp_source_template, \@search_array);

我想要返回$ find_string和regex_replacements哈希引用 . 应该注意的是,在sub中我需要在@search数组元素的末尾追加$ excel_lookup的值 .

我不明白该怎么做的一点是构建变量级哈希查找 .

1 回答

  • 1

    您可以尝试使用Data::Diver它提供对深层嵌套结构元素的简单访问 .

    例如:

    use feature qw(say);
    use strict;
    use warnings;
    use Data::Diver qw(Dive);
    
    my $hash = { a => { b => 1, c => { d => 2 }}};
    my $keys = [[ 'a', 'b'], ['a','c','d']];
    lookup_keys( $hash, $keys );
    
    sub lookup_keys {
        my ( $hash, $keys ) = @_;
    
        for my $key ( @$keys ) {
            my $value = Dive( $hash, @$key );
            say $value;
        }
    }
    

    Output

    1
    2
    

    See Also

相关问题