我將下面的YAML流加載到Perl的數組中,我想遍歷與Field2相關的數組。在Perl中,如何遍歷由YAML加載的散列數組?
use YAML;
my @arr = Load(<<'...');
---
Field1: F1
Field2:
- {Key: v1, Val: v2}
- {Key: v3, Val: v4}
---
Field1: F2
Field2:
- {Key: v5, Val: v6}
- {Key: v7, Val: v8}
...
foreach (@arr) {
@tmp = $_->{'Field2'};
print $#tmp; # why it says 0 when I have 2 elements?
# Also why does the below loop not work?
foreach ($_->{'Field2'}) {
print $_->{'Key'} . " -> " $_->{'Val'} . "\n";
}
}
我很欣賞任何反饋。謝謝。
你沒有解決Martin詢問'Field2'鍵的hashref值的部分。 – daxim 2010-09-03 16:13:59
@daxim鏈接解決了與參考相關的問題。 – 2010-09-03 16:59:58