繁体   English   中英

在 perl 中迭代一个 HASH

[英]Iterating through a HASH in perl

我使用了: print Dumper $decoded ,其中$decoded是 HASH 变量,我得到的是 output。

    $VAR1 = {
              'Case_345' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   },
              'Case_512' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   },
              'Case_534' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   },
              'Case_552' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   },
              'Case_578' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   },
              'Case_466' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   },
              'Case_754' => {
                                     'Notes' => 'test2',
                                     'Submit_Date' => '2015-11-21 00:53:22 UTC',
                                     'Last_Resolved_Date' => ''
                                   }
            };

这是我尝试使用 output 的一些值来测试遍历 HASH 的方法。

    foreach my $key ( keys %decoded )
    {
         print "key: $key, value: $decoded{$key}\n";
    }

它根本不会遍历$decoded变量。 我是 perl 的新手,有人可以帮帮我吗? 如果有帮助,我通过使用JSON::XS::decode_json($json_str)转换字符串得到了这个。 谢谢。

第一件事是$decoded是一个包含哈希引用标量变量 ,与哈希变量%decoded无关。 要访问$decoded引用的哈希,必须使用%$decoded 取消引用。

必须始终在编写的每个 Perl程序的顶部始终 use strict use warnings 'all'并在use warnings 'all' ,尤其是在向他人寻求帮助之前。 在这种情况下,这是一个简单的度量,它会捕获到您使用%decoded而不声明它的事实。

此外,您的散列更哈希引用,并打印出来会产生一种不到这样有用

key: Case_466, value: HASH(0xbf4840)
key: Case_534, value: HASH(0xc6dc00)
key: Case_552, value: HASH(0x280fce0)
key: Case_512, value: HASH(0xbf4930)
key: Case_345, value: HASH(0xd4c2f0)
key: Case_754, value: HASH(0x280fdd0)
key: Case_578, value: HASH(0x280fd58)

大概您想要每个哈希中所有字段的值? 要访问给定键和对哈希的引用的哈希元素的值,应编写$hashref->{key} ,因此此代码将为您转储整个哈希

for my $key ( keys %$decoded ) {
  my $data = $decoded->{$key};
  print "key: $key, values:\n";
  for my $field ( qw/ Submit_Date Last_Resolved_Date Notes / ) {
    printf "    %-18s => %s\n", $field, $data->{$field};
  }
}

输出

key: Case_578, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2
key: Case_754, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2
key: Case_534, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2
key: Case_345, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2
key: Case_512, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2
key: Case_552, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2
key: Case_466, values:
    Submit_Date        => 2015-11-21 00:53:22 UTC
    Last_Resolved_Date => 
    Notes              => test2

首先,正如Borodin所说, $decoded不是哈希变量。 这是一个标量变量; 它恰好包含对匿名哈希的引用。 在Perl中,每个标记都有其自己的命名空间,因此标量变量$decoded和哈希变量%decoded (或数组变量@decoded )之间没有关系。 如果您在use strict;条件下运行,则对未定义哈希变量的使用将被标记为错误use strict; ; 通常,您应该始终use strict;启动Perl脚本use strict; 将警告与use warnings;一起打开是个好主意use warnings; 也一样

人们普遍认为use strict;的设计错误use strict; 在Perl 5中默认情况下未启用该功能。在5.12版中已将其默认设置为启用,但是为了向后兼容,该功能仍仅对于声明已为新的Perl编写的脚本启用。 因此作为use strict;的替代选择use strict; ,您可以只use v5.12; (或更新的版本号),这还将使其他(非实验性的)新功能(例如say以换行符打印。

其次,在Perl中迭代哈希的键和值的标准方法是使用each ,您既可以对外部哈希又可以对每个散列进行操作:

while (my ($case, $data) = each %$decoded) {
  print "$case:\n";
  while (my ($field, $value) = each %$data) {
    print "\t$field: $value\n";
  }
}

但是在实践中,通常会避免使用each方法,因为它们不会以任何可预测的顺序遍历键(尽管keysvalues返回其结果的顺序相同)。 您可能更喜欢对键进行排序并手动查找值:

foreach my $case (sort keys %$decoded) {
  my $data = $decoded->{$case};     
  print "$case:\n";
  foreach my $field (sort keys %$data) {
    $value = $data->{$field};
    print "\t$field: $value\n";
  }
}

同样,输出结果可能更友好,使用空格而不是下划线:

foreach my $case (sort keys %$decoded) {
  my $data = $decoded->{$case};␣␣␣␣␣
  (my $case_label = $case) =~ s/_/ /g;
  print "$case_label:\n";
  foreach my $field (sort keys %$data) {
    (my $field_label = $field) =~ s/_/ /g;
    $value = $data->{$field};
    print "\t$field_label: $value\n";
  }
}

产生此:

Case 345:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC
Case 466:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC
Case 512:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC
Case 534:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC
Case 552:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC
Case 578:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC
Case 754:
    Last Resolved Date: 
    Notes: test2
    Submit Date: 2015-11-21 00:53:22 UTC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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