简体   繁体   中英

Perl - “Complex” Data Structure

I'm trying to get a workable data structure that I can pull the element values from in a sensible fashion. Just having great difficulty working with the data once its in the structure. This is how the struct is built:

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";}

}

Producing this structure:

%Master contains:

$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}

Although ideally the structure should look like this but I had even less joy trying to pull data out at sub identifyNode:

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

Then to get back at the data in another method I'm using:

 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++;
    }

}

This then generates:

$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

So for each hash in $h only the topmost host gets evaluated and hostname returned. This is because it is told to do so by the [0] in line:

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

I've played around with different data structures and de-referencing the structure a multitude of ways and this is the only halfway house I've found...

(The IPs have been obfuscated so are not consistent in my examples)

Thanks for your advice it got me halfway there. It works now (in still somewhat a convoluted fashion!) :

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);
}

Am I brave enough to add the data as a hash to @fin_nodes.. hmm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM