Perl - “复杂”数据结构

时间:2017-11-09 13:58:33

标签: perl data-structures

我正在尝试获得一个可行的数据结构,我可以以合理的方式提取元素值。在结构中处理数据时遇到很大的困难。这就是结构的构建方式:

sub hopCompare
{

    my %count;
    my %master;
    my $index = 0;

    foreach my $objPath (@latest)    #get Path object out of master array
    {
            my @path = @{$objPath->_getHopList()}; #dereferencing
            my $iter = 0;
            foreach my $hop (@path)
            {

                    ++$count{$hop}->{FREQ};
                    $count{$hop}->{INDEX} = $index;
                    $count{$hop}->{NODE} = $hop;

                    $index++;

            }
            $index = 0;
    }
    foreach my $element( keys %count )
    {
            if (defined($count{$element}->{NODE}))
            {
                    my $curr = $count{$element}->{INDEX};
                    my $freq = $count{$element}->{FREQ};
                    if (($freq > 1) || ($count{$element}->{INDEX} =~ /[0-1]/))
                    {
                            push @{ $master{$curr} }, {$count{$element}->{NODE}, {FREQ => $count{$element}->{FREQ}}};
                    }
                    print "$element = $count{$element}\n";
                    print "$element Index = $count{$element}->{INDEX}\n";
            }
    }
    print "\n Master contains: \n" . Dumper (%master);
    if (%master){return %master;} else {die "NO FINAL HOPS MATCHED";}

}

制作此结构:

%Master包含:

$VAR1 = '4';
$VAR2 = [
      {
        '1.1.1.2' => {
                              'FREQ' => 2
                            }
      }
    ];
$VAR3 = '1';
$VAR4 = [
      {
        '1.1.1.9' => {
                              'FREQ' => 5
                            }
      },
      {
        '1.1.1.8' => {
                              'FREQ' => 1
                            }
      }
    ];

    {truncated}

虽然理想情况下结构看起来应该是这样的,但是我试图在sub identifyNode上提取数据的努力更少:

$VAR1 = {
      '1' => [
               {
                 '1.1.1.9' => {
                                       'FREQ' => 5
                                     }
               },
               {
                 '1.1.5.8' => {
                                       'FREQ' => 1
                                     }
               }
             ],

然后以我正在使用的另一种方法取回数据:

 sub identifyNode
 {
    my %hops = %{$_[0]};
    my $i = 0;

    foreach my $h ( keys %hops )                   #The HOP-INDEX is the key
    {
            print "\n\$h looks like \n" . Dumper ($hops{$h});
            my %host = %{ $hops{$h}[0] };           #Push the first HASH in INDEX to the %host HASH
            foreach my $hip (keys %host)
            {
                            my $corelink = `corelinks $hip`;

                            my ($node) = $corelink =~ /([a-z0-9-]+),[a-z0-9-\/]+,$hip/s;
                            print "\n\t\t\tHostname is $node\n";
            }
            $i++;
    }

}

然后生成:

$h looks like
$VAR1 = [
      {
        '1.1.1.2' => {
                              'FREQ' => 2
                            }
      }
    ];

                    Hostname is blabla-bla-a1

$h looks like
$VAR1 = [
      {
        '1.1.1.9' => {
                              'FREQ' => 5
                            }
      },
      {
        '1.1.1.8' => {
                              'FREQ' => 1
                            }
      }
    ];

                    Hostname is somew-some-a1

因此,对于$ h中的每个哈希,只会评估最顶层的主机并返回主机名。这是因为它被[0]在行中告知:

my %host = %{ $hops{$h}[0] };

我玩过不同的数据结构并以多种方式取消参考结构,这是我找到的唯一一个中途的房子......

(IP已被混淆,因此在我的示例中不一致)

1 个答案:

答案 0 :(得分:0)

感谢您的建议,它让我到了一半。它现在有效(仍然有点复杂!):

sub identifyNode
{
    my %hops = %{$_[0]};
    my $i = 0;
    my @fin_nodes;
    my $hindex;

    foreach my $h ( keys %hops )                   #The HOP-INDEX is the key
    {
            $hindex = $h;

            foreach my $e (@{$hops{$h}})       #first part of solution credit Zdim
            {
                    my @host = %{ $e };      #second part of solution
                    my $hip = $host[0];
                    my $corelink = `corelinks $hip`;
                    my ($node) = $corelink =~ /([a-z0-9-]+),[a-z0-9-\/]+,$hip/s;
                    print "\n\t\t\tHostname is $node\n";

                    push (@fin_nodes, [$node, $hindex, $e->{$hip}->{FREQ}]);
            }
            $i++;
    }
    return (\@fin_nodes);
}

我是否足够勇敢地将数据作为哈希添加到@fin_nodes .. hmm