鑑於下面的代碼,我想弄清楚如何遍歷哈希引用數組(或者至少我認爲它是一個哈希引用數組)。那就是$Policy->rules()
?如何迭代這個Perl結構?
也許有創建一個結構數組的更好方法,我願意提供建議。
use Class::Struct;
use Data::Dumper;
struct Policy => {
listings => '@', # Will treat like rules eventually.
rules => '@', # an array of rules
};
struct Rule => {
direction => '$',
id => '$',
};
$policy = Policy->new();
$rule1 = Rule->new();
$rule1->direction('Any');
$rule1->id(1);
$rule2 = Rule->new();
$rule2->direction('Inbound');
$rule2->id(2);
$rule3 = Rule->new();
$rule3->direction('Outbound');
$rule3->id(3);
push($policy->rules(),$rule1);
push($policy->rules(),$rule2);
push($policy->rules(),$rule3);
$Data::Dumper::Indent = $Data::Dumper::Terse = 1;
print Dumper \$policy;
輸出:
\bless({
'Policy::listings' => [],
'Policy::rules' => [
bless({
'Rule::id' => 1,
'Rule::direction' => 'Any'
}, 'Rule'),
bless({
'Rule::id' => 2,
'Rule::direction' => 'Inbound'
}, 'Rule'),
bless({
'Rule::id' => 3,
'Rule::direction' => 'Outbound'
}, 'Rule')
]
}, 'Policy')
嗨,謝謝你的幫忙。我不得不調整一下。 Visit_listing()被稱爲2x。在將visit_rule更改爲獲取$ rule-> direction和$ rule-> id之後,當它嘗試解析$ rule-> direction時,出現以下錯誤:無法在第66行的未指向引用上調用方法「direction」。 – user1637240
應用您提到的修復程序。測試它。不要得到你得到的錯誤,也不要對你提供的信息有意義。 – ikegami
我如何獲得參考祝福? – user1637240