我想要找出散列中的鍵的數量和值的數量並打印這些數字。我寫了這樣的代碼,但它沒有給出鍵的數量。我的代碼中有什麼錯誤?散列中的鍵的數量
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML::Reader;
my $file;open $file, 'formal.xml');
my $reader = XML::LibXML::Reader->new(IO => $file) or die ("unable to open file");
while ($reader->nextElement('DATA')) {
my $info = $reader->readOuterXml();
$reader->nextElement('Number');
my $number = $reader->readInnerXml();
print("num: $number\n");
print(" datainfo: $info\n");
如何將這些num和datainfo存儲在散列中?我如何計算散列鍵的數量?我試過這樣但它不起作用。
my %nums =("$number", $info);
while ((my $keys, my $values) = each (%nums)) {
print ("NUMBER:$keys." =>"INFORMATION: ".$values." \n");
}
my $key_count = keys %nums;
print "$key_count";
}
close($file);
}
當我試圖執行它時,它只給出一個數字,但我有更多的數字。也許我的哈希包含一個數字,但我如何迭代我的哈希來存儲更多的數字?
您能否提供inpit數據的示例? – Andrey
等待您在閱讀器while循環內重新初始化散列?在相同的循環中枚舉它的內容呢?這很混亂。這聽起來不像這些問題與你的問題有任何關係,但它們導致你無法獲得正確的輸出。 – dlamblin