访问散列指针数组的数组

时间:2016-08-08 15:39:43

标签: perl multidimensional-array reference yaml

这是我的YAML文件

  standalone_execution:  
  - utpsm_executable: &seid_01   
     name: tpsm_BE         
     rulesfile: [*rulesid_02,*rulesid_01]
     target_list: [*runid_01]

  - utpsm_executable: &seid_02
     name: tpsm_LE         
     rulesfile: [*rulesid_01,*rulesid_02]
     target_list: [*runid_02,*runid_01]

  rules_file: 
  - rules_file_id: &rulesid_01 
     name: tpsm_rulesfile_LE 
     hostname:       

  - rules_file_id: &rulesid_02
     name: tpsm_rulesfile_BE        
     hostname:  

  run_target_platforms: 
  - run_target_id: &runid_01
     target_connection_info: 
      run_target_hostname: 
      run_target_username: 
      run_target_password: 

  - run_target_id: &runid_02
     target_connection_info: 
      run_target_hostname: 
      run_target_username: 
      run_target_password: 

这是我解析Yaml的方式:

   $yaml_input = YAML::XS::LoadFile("$input_file");
   Yaml_Lib::parse_yaml($yaml_input);

这是我从中提取参数的函数

sub parse_yaml_standalone($) {
    my ($yaml_input) = @_;

    $standalone_exec = $yaml_input->{standalone_execution};

    @utpsm_exec    = ();
    @se_exec_name  = ();
    @se_tgt_list   = ();
    @se_rulesfile  = ();
    @se_rules_name = ();

    @se_run_target_controls = ();
    @se_run_target_controls = ();

    my $k = 0;
    my $j = 0;
    my $i = 0;

    for my $seindex ( @$standalone_exec ) {

        $utpsm_exec[$k]   = $seindex->{utpsm_executable};
        $se_exec_name[$k] = $utpsm_exec[$k]->{name};
        $se_tgt_list[$k]  = $utpsm_exec[$k]->{target_list};
        $se_rulesfile[$k] = $utpsm_exec[$k]->{rulesfile};

        for my $rindex ( @{ $se_rulesfile[$k] } ) {
            $se_rules_name[$j] = $rindex->{name};
            $j++;
        }

        for my $run_index ( @{ $se_tgt_list[$k] } ) {
            $se_run_target_controls[$i] = $run_index->{target_controls};
            $se_run_target_types[$i]    = $se_run_target_controls[$i]->{run_target_type};
            $se_run_target_active[$i]   = $se_run_target_controls[$i]->{run_target_active};
            $i++;
        }

        $k++;

    }    #end of main for

    print "yaml rules: @se_rules_name\n";
}

这里,每个target_list元素应该与每个rulesfile元素一起运行,所以我写下面的代码但是对于index = 1,我没有得到欲望的结果,任何人都可以纠正我在哪里出错了吗?

my $i = 0;
my $j = 0;

foreach my $se_index ( @{$Yaml_Lib::standalone_exec} ) {
    print "parent hash : $se_index\n";

    foreach my $se_runid ( @{ $Yaml_Lib::se_tgt_list[$i] } ) {

        my $pid = fork();
        die "INFO :: Cannot fork new process for run target on host : $!" unless defined($pid);

        # In Child Processing
        if ( $pid == 0 ) {

            my $k    = 0;
            my $cpid = ($$);
            print "INFO :: In run target : child $j process , pid : ($$)\n";
            Client_Lib::client_logs($cpid);

            #send the tpsm exec($i in this case) details as well with run targets(by $j)
            print "child hash $se_index\n";

            foreach my $rindex ( @{ $Yaml_Lib::se_rulesfile[$i] } ) {
                print "rules array : $rindex\n";
                Client_Lib::cap_prints( $Client_Lib::prints_file, "$Yaml_Lib::se_rulesfile[$i]\n" );
                $k++;
            }

            #exit from child process

            exit 0;
        }

以下代码基本上是等待孩子终止的父母以及由于问题的长度而未显示的其他检查。

请让我知道我哪里出错。

1 个答案:

答案 0 :(得分:0)

在您的YAML文件中,您对根本未定义的锚点使用别名(rulesid_03rulesid_04)或YAML流中的别名之后的某个地方使用别名(rulesid_01,其他) 。两者都会导致YAML无效。

此外,你有缩进问题。 run_target_platforms的缩进比前一个键rules_filestandalone_execution的缩进程度要小。这也是无效的。最后,这可能不符合您的要求:

- run_target_id: &runid_01
  target_connection_info: 
  run_target_hostname: 
  run_target_username: 
  run_target_password:

这是一个包含映射的列表项。映射的第一个键是run_target_id,其对应的值为空,但使用锚runid_01进行注释。您可能希望将此映射中的其余键作为子映射的一部分,这是第一个键的值。

这是一个修改过的YAML,带有适当的锚点,别名和缩进:

rules_file: 
- rules_file_id: &rulesid_01 
    name: tpsm_rulesfile_LE 
    hostname:       

- rules_file_id: &rulesid_02
    name: tpsm_rulesfile_BE        
    hostname:  

run_target_platforms: 
- run_target_id: &runid_01
    target_connection_info: 
    run_target_hostname: 
    run_target_username: 
    run_target_password: 

- run_target_id: &runid_02
    target_connection_info: 
    run_target_hostname: 
    run_target_username: 
    run_target_password: 

standalone_execution:  
- utpsm_executable: &seid_01   
    name: tpsm_BE         
    rulesfile: [*rulesid_02,*rulesid_01]
    target_list: [*runid_01]

- utpsm_executable: &seid_02
    name: tpsm_LE         
    rulesfile: []
    target_list: [*runid_02]

请注意,我删除了*rulesid_03,*rulesid_04,因为它们不存在。您当然可以使用这些锚点添加内容,然后重新添加别名。