在Perl中,如何在遍历哈希时跳过空键?

时间:2012-04-25 19:30:10

标签: perl hash traversal

这是我的问题,我对Perl知之甚少,而且我有这个功能需要修复。

调用此函数deviceModelMenu()时,CLI将显示以下文本:

The following models are available
==================================================
1.   
2. Cisco1240 
3. Catalyst3750 
4. Catalyst3650 
5. HP2524 

第一项是空的,这是错误的,我需要解决这个问题,显示此菜单的代码片段是:

my $features = shift;
print "=" x 50, "\n";
print "The following models are available\n";
print "=" x 50, "\n";
my $i=1;
foreach (keys %{$features->{features}[0]->{deviceModel}})
{
    print "$i. $_ \n";
    $i++;
}

如果我添加以下行:

warn Dumper($features->{features}[0]->{deviceModel});

它转储了这个:

$VAR1 = {
      'deviceModel' => {
                         '' => {
                                 'cfg' => []
                               },
                         'Cisco1240' => {
                                        'cfg' => [
                                                 'cisco1240feature.cfg'
                                               ]
                                      },
                         'Catalyst3750' => {
                                           'cfg' => [
                                                    'catalyst3750feature.cfg'
                                                  ]
                                         },
                         'Catalyst3650' => {
                                           'cfg' => [
                                                    'catalyst3650feature.cfg'
                                                  ]
                                         },
                         'HP2524' => {
                                     'cfg' => [
                                              'hp2524feature.cfg'
                                            ]
                                   }
                       }
    };

您可能会注意到,第一项确实是空的。我添加了以下行来跳过它,只打印其余的信息:

if ($_ eq '') {
        shift;
}

但似乎 work 做我想做的事。如果项目为空,我想跳过该项目。

3 个答案:

答案 0 :(得分:6)

好吧,将@ARGV(隐式参数转换为主程序中的转移)转移@_(函数中隐含的shift参数)对你没有帮助,因为你是不打印其中任何一个。

你可以:

  1. 首先不添加''条目(取决于它是如何生成的)

  2. 打印前删除''条目:

    delete $features->{features}[0]->{deviceModel}->{''};
    
  3. 不要打印条目:

    if($_ eq '') {
        next;
    }
    

    if($_ ne '') {
        print "$i. $_ \n";
        $i++;
    }
    

答案 1 :(得分:4)

foreach (keys %{$features->{features}[0]->{deviceModel}})
{
    next unless length($_);
    print "$i. $_ \n";
    $i++;
}

答案 2 :(得分:3)

#!/usr/bin/env perl

use strict; use warnings;

my $devices = {
    'deviceModel' => {
         '' => { 'cfg' => [] },
         'Cisco1240' => { 'cfg' => ['cisco1240feature.cfg' ] },
         'Catalyst3750' => { 'cfg' => [ 'catalyst3750feature.cfg' ]},
         'Catalyst3650' => { 'cfg' => [ 'catalyst3650feature.cfg' ]},
         'HP2524' => { 'cfg' => [ 'hp2524feature.cfg' ]},
     }
};


{
    my $item = 1;
    for my $d (grep length, keys %{ $devices->{deviceModel} }) {
        printf "%2d. %s\n", $item++, $d;
    }
}

输出:

 1. Catalyst3750
 2. Cisco1240
 3. Catalyst3650
 4. HP2524