取消引用多级哈希:一个实际的例子

时间:2011-07-23 04:51:25

标签: perl hash dereference multi-level

我有一个数据,我将这个数据泵入这个多级哈希:

$newcomm_stat_hash{$stat_message_class}{$stat_process} = $stat_host;

我可以打印出$stat_message_class$stat_process 键值结构:

foreach my $stat_message_class (keys %newcomm_stat_hash) {

   my $stat_message_type = $stat_message_class;

   foreach my $stat_process (keys %{$newcomm_stat_hash{$stat_message_class}} ) {

      print $stat_host;
   }
}

但是当我按照相同的格式打印$stat_host values时(请参阅下面的代码),我收到此错误消息:

  

在multilevel_hash第24行使用“strict refs”时,不能使用字符串(“dc109”)作为HASH引用。

我得到了关键或值函数的相同信息。

#!/usr/bin/perl
use warnings; 
use strict;

my %newcomm_stat_hash; 
my $control_server = "dc100";
my $control_stat_message = "OCCD2o";

$newcomm_stat_hash{'OCCD2o'} =  { 'filesrvr' => 'dc100',
                                  'dhcpsrv'  => 'dc100',
                                  'dnssrv'   => 'dc109',
                                  'mailpfd'  => 'dc100',
                                };

$newcomm_stat_hash{'PIDmon2'} = { 'pingstat' => 'fg100',
                                  'udpmon'   => 'fg100',
                                  'ftp'      => 'dc100',
                                  'casper'   => 'dc440',
                                };

foreach my $stat_message_class ( keys %newcomm_stat_hash ) {

 my $stat_message_type = $stat_message_class;

 foreach my $stat_process ( keys %{$newcomm_stat_hash{$stat_message_class}} ) {

         foreach my $stat_host (keys %{$newcomm_stat_hash{$stat_message_class}{$stat_process}} ) {

             print $stat_host;
         } 
     }
}

在将多级哈希取消引用到$stat_host之后,我想在最后插入这个:

use TERM::ANSIColor;

if ($stat_host ne $control_server) {

    print "$stat_host, $stat_process , $stat_message_class";   
}   

elsif (  ($stat_host ne $control_server)
      && ($stat_message_class eq $control_stat_message)
      ) {   

    print color 'red';   
    print "$stat_host, $stat_process , $stat_message_class";
    print color 'reset';   
}

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的代码,您说OCCD2o是“邮件类”,filesrvr是“进程”,dc100是“主机”。如果是这种情况,则最内层的“foreach”循环不是必需的,因为您已经处于hash-of-hashrefs的“主机”级别。你不能在那个层面上更深入。

因此,如果您将表达式%{$newcomm_stat_hash{$stat_message_class} {$stat_process}}重写为:

$tmp = $newcomm_stat_hash{$stat_message_class}{$stat_process}
$hash = %{$tmp}

然后$tmp被评估为字符串标量dc109,它不能被解除引用为哈希值,从而显示错误消息。

我想说这是正确的循环结构:

foreach my $stat_message_class(keys %newcomm_stat_hash){
    my $stat_hash = $newcomm_stat_hash{$stat_message_class};
    my $stat_message_type = $stat_message_class;
    foreach my $stat_process (keys %{$stat_hash}){
        my $stat_host = $stat_hash->{$stat_process};

        print $stat_message_class, " / ", $stat_process, " / ", $stat_host, "\n";
    }
}

答案 1 :(得分:0)

这里的目标看起来是打印在红色对应$control_server$control_stat_message的哈希条目中。

如果是这样,条件将不会按照您的意愿执行,因为elsif子句永远不会执行。

#!/usr/bin/perl
use warnings; 
use strict;
use Term::ANSIColor;

my %newcomm_stat_hash; 
my $control_server       = "dc100";
my $control_stat_message = "OCCD2o";

$newcomm_stat_hash{'OCCD2o'} =  { 'filesrvr' => 'dc100',
                                  'dhcpsrv'  => 'dc100',
                                  'dnssrv'   => 'dc109',
                                  'mailpfd'  => 'dc100',
                                };

$newcomm_stat_hash{'PIDmon2'} = { 'pingstat' => 'fg100',
                                  'udpmon'   => 'fg100',
                                  'ftp'      => 'dc100',
                                  'casper'   => 'dc440',
                                };

foreach my $stat_message_class ( keys %newcomm_stat_hash ) {

    my $message_class = $newcomm_stat_hash{$stat_message_class};

    foreach my $stat_process ( keys %{ $message_class } ) {

        my $host = $message_class->{$stat_process};

        my $info = join(', ', $stat_message_class, $stat_process, $host) . "\n";

        if (   $stat_message_class eq $control_stat_message
           &&  $host eq $control_server
           )
        {

            print colored ( $info, 'red' );  # Prints $info in red
        }

        else {

            print $info;                     # Prints $info normally
        }
    }
}