【问题标题】:Accessing each hash member of array -- Perl访问数组的每个散列成员——Perl
【发布时间】:2013-04-13 01:08:25
【问题描述】:

我正在尝试访问存储在 Perl 数组中的哈希的每个成员。我已经看过Looping through an array of hashes in Perl,我需要更好的方法来访问它们。我的最终结果是将每个项目(哈希)拆分为一个数组,将该哈希的键存储在一个新数组中,将该数组的值存储在另一个数组中。

编辑:我改变了他们获取数据的方式,以更好地反映我的代码。我认为这无关紧要,但确实如此。

XML 源:

<XML>
    <computer>
        <network>
            <interface1>
                <name>eht0</name>
                <ip>182.32.14.52</ip>
            </interface1>
            <interface2>
                <name>eth2</name>
                <ip>123.234.13.41</ip>
            </interface2>
        </network>
    </computer>
</xml>

PERL 代码:

my %interfaceHash;
for(my $i=0; $i < $numOfInterfaces; $i++ ){
    my $interfaceNodes = $xmldoc->findnodes('//computer/network/interface'.($i+1).'/*');
    foreach my $inode ($interfaceNodes->get_nodelist){
        my $inetValue = $inode->textContent();
        if ($inetValue){
            my $inetField = $inode->localname;
            push (@net_int_fields, $inetField);
            push (@net_int_values, $inetValue);
        }
    }
    for(my $i =0; $i< ($#net_int_fields); $i++){
        $net_int_hash{"$net_int_fields[$i]"} = "$net_int_values[$i]";
    }
    push (@networkInterfaces, \%net_int_hash); #stores 
}

现在,如果我尝试访问该数组,它会清除散列中存储的内容。

【问题讨论】:

  • 我想我知道我需要做什么了。我创建了一个创建数组副本的函数(使用我发送的取消引用数组和哈希)。我利用了你的回答,@choroba。

标签: xml arrays perl hash


【解决方案1】:

要在 Perl 中构建复杂的数据结构,请从阅读 perldsc - Perl Data Structures Cookbook 开始。

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

my %interface1 = (iFaceName => 'eth0',
                  ipAddr    => '192.168.0.43',
                 );
my %interface2 = (iFaceName => 'eth1',
                  ipAddr    => '192.168.10.64',
                 );
my @networkInterfaces;
my @iFaceKeys;
my @iFaceVals;
push @networkInterfaces, \%interface1; # Note the backslash!
push @networkInterfaces, \%interface2;

for my $iFace (@networkInterfaces) {
    push @iFaceKeys, keys   %$iFace;
    push @iFaceVals, values %$iFace;
}

【讨论】:

    【解决方案2】:

    您的问题是哈希不能是数组的成员。只有标量可以。

    数组中需要的不是散列,而是散列的标量引用

    push @networkInterfaces \%interface1; # \ is a reference taking operator
    

    然后,要访问该 hashref 的单个元素,您可以这样做

    $networkInterfaces[0]->{iFaceName}; # "->{}" accesses a value of a hash reference
    

    要访问整个哈希(例如,获取其密钥),请使用 %{ hashref } 语法取消引用:

    foreach my $key ( keys %{ $networkInterfaces[0] } ) {
    

    【讨论】:

      【解决方案3】:

      这样做的一个好方法是创建一个哈希引用数组。

      %first_hash = (a => 1, b => 2);
      %second_hash = (a => 3, b => 4);
      
      @array_of_hash_refs = ( \%first_hash, \%second_hash );
      
      foreach $hash_ref (@array_of_hash_refs)
      {
              print "a = ", $hash->{a}, "\n";
              print "b = ", $hash->{b}, "\n";
      }
      

      请记住,perl 访问散列数据的语法不同,具体取决于您使用的是散列对象还是对散列对象的引用。

      $hash_ref->{key}
      $hash{key}
      

      【讨论】:

      • 这很有帮助,但我需要在我的问题中编辑我的代码。我实际上是通过循环添加哈希值,所以我每次都必须清空这些值。每次我清空这些值时,我的哈希只有最后一个哈希的多个条目。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2012-08-03
      • 2013-01-29
      • 2013-02-05
      • 1970-01-01
      • 2012-03-24
      • 2017-02-21
      相关资源
      最近更新 更多