如何遍历对Perl中哈希数组的引用?

Joo*_*oon 4 arrays perl hash perl-data-structures

我引用了一个hases数组,我将其传递给perl脚本中的子例程

这是代码:

sub mySub {
    (my $resultref) = @_;
    my @list = @$resultref;
    print Dumper(@list);
    foreach my $result (@list) {
        print Dumper($result);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

$VAR1 = [
          {
            'portName' => '1.1',
            'ips' => [
                       '192.168.1.242'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        '00:16:76:9e:63:47'
                      ]
          },
          {
            'portName' => '1.10',
            'ips' => [
                       '192.168.1.119',
                       '192.168.1.3'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        'd0:67:e5:f8:7e:7e',
                        'd0:67:e5:f8:7e:76'
                      ]
          },
        ];

$VAR1 = [
          {
            'portName' => '1.1',
            'ips' => [
                       '192.168.1.242'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        '00:16:76:9e:63:47'
                      ]
          },
          {
            'portName' => '1.10',
            'ips' => [
                       '192.168.1.119',
                       '192.168.1.3'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        'd0:67:e5:f8:7e:7e',
                        'd0:67:e5:f8:7e:76'
                      ]
          },
        ];
Run Code Online (Sandbox Code Playgroud)

循环将整个数组放入$ result变量中.我尝试将其解除引用为@ $ result [0]但没有成功.

如何单独循环这些哈希?

谢谢!

TLP*_*TLP 5

到的参数数据::自卸车Dumper功能应该是引用.例如:

use Data::Dumper;
my @array = ([1,2,3], [11,22,33]); # Two-dimensional array
print Dumper @array;               # print array
print Dumper \@array;              # print reference to array
Run Code Online (Sandbox Code Playgroud)

输出:

$VAR1 = [
          1,
          2,
          3
        ];
$VAR2 = [
          11,
          22,
          33
        ];

$VAR1 = [
          [
            1,
            2,
            3
          ],
          [
            11,
            22,
            33
          ]
        ];
Run Code Online (Sandbox Code Playgroud)

第二个印刷品在一个变量中给出了整个结构.当你直接打印数组时,它会扩展到它的所有元素,所以...

print Dumper @array;
Run Code Online (Sandbox Code Playgroud)

相当于:

print Dumper $array[0], $array[1], ..., $array[$#array];
Run Code Online (Sandbox Code Playgroud)

所以,在你的情况下,只需:

sub mySub {
    my ($resultref) = @_;
    print Dumper $resultref;
}
Run Code Online (Sandbox Code Playgroud)

访问内部变量:

看看Data::Dumper输出:

$VAR1 = [    # bracket denotes start of an array ref
          {  # curly brackets = hash ref
            'portName' => '1.1',
            'ips' => [
                       '192.168.1.242'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        '00:16:76:9e:63:47'
                      ]
          }, # hash ref ends, comma = new array element begins
          {  # new hash ref 
            'portName' => '1.10',
            'ips' => [
                       '192.168.1.119',
                       '192.168.1.3'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        'd0:67:e5:f8:7e:7e',
                        'd0:67:e5:f8:7e:76'
                      ]
          }, # end of hash
        ];   # end of array
Run Code Online (Sandbox Code Playgroud)

这里要注意的重要一点是数组的所有元素,以及散列的所有值都是标量.因此,所有哈希和数组都可以轻松地分解为标量列表.

for my $aref (@$resultref) {  # starting array ref
    for my $aref2 (@$aref) {  # second level array ref
        for my $href (@$aref2)  # here begins the hash
            local $\ = "\n";    # add newline to print for simplicity
            print $href->{portName};    # printing a scalar
            print for @{$href_>{ips}};  # printing an array ref w post-script loop
            print $href->{switchIp};
            print for @{$href->{macs}};
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意使用箭头运算符取消引用引用.如果您有哈希或数组,$array[0]或者$hash{$key}使用引用,则"指向"引用中包含的地址:$array->[0]或者$hash->{$key}.