对于练习我使用的是我发现的设计哈希,并尝试添加新的键值对,但是perl报告"编译错误:(F)可能意味着你有语法错误" 哈希是
my $selected = 'box';
my $d = $design->{$selected};
my $design = {
box => {
ne => {data => 'north-east'},
nw => {data => 'north-west'},
n => {data => 'north'},
s => {data => 'south'},
e => {data => 'east'},
w => {data => 'west'},
se => {data => 'south-east'},
sw => {data => 'south-west'},
}
};
并添加一个新的键/值对我尝试了所有语法,我可以来acoss:
%{$design} = eastside=>ne;
$design{box}->{eastside}=Data=>ne;
%{$design}{box}={eastside=>Data=>ne};
这些都没有奏效(以及这些的变化)。这是因为它是一个复杂的哈希吗?
答案 0 :(得分:0)
以下是尝试使用语法更正添加到哈希的三行的重写。它可能不是您想要的确切代码,但它不应再抛出错误。
#%{$design} = eastside=>ne;
$design{eastside} = 'ne';
#$design{box}->{eastside}=Data=>ne;
$design{box}{eastside} = {data => 'ne'};
#%{$design}{box}={eastside=>Data=>ne};
$design{box} = {eastside => {Data => 'ne'}};
由于perl生锈,我最初的想法可能是基础: 它看起来像哈希,$ design,你开始使用的是Dumper的输出,而不是用于定义哈希的实际perl语法。
尝试像这样定义哈希:
use Data::Dumper;
my %design
$design{'box'}{'ne'} = {'data' => 'north-east'};
$design{'box'}{'n'} = {'data' => 'north'};
$design{'box'}{'nw'} = {'data' => 'north-west'};
print $design{'box'}{'nw'}{'data'};
print "\n";
print Dumper(\%design);
应输出:
north-west
$VAR1 = {
'box' => {
'n' => {
'data' => 'north'
},
'ne' => {
'data' => 'north-east'
},
'nw' => {
'data' => 'north-west'
}
}
};