Reputation: 1
For practice I am using a contrived hash I found, and attempting to add new key value pairs, but perl reports "compilation error: (F) Probably means you had a syntax error" The hash is
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'},
}
};
and to add a new key/value pair I have tried all the syntax I could come acoss:
%{$design} = eastside=>ne;
$design{box}->{eastside}=Data=>ne;
%{$design}{box}={eastside=>Data=>ne};
None of these worked (and variations of these). Is it due the fact that it is a complex hash?
Upvotes: 0
Views: 63
Reputation: 413
Here are rewrites of your three lines attempting to add to the hash with the syntax corrected. It may not be the exact code you want, but it shouldn't throw an error any longer.
#%{$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'}};
My original thought was probably off base due to being rusty with perl: It looks like the hash, $design, you're starting with is the output from Dumper rather than the actual perl syntax for defining a hash.
Try defining the hash like this:
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);
That should output:
north-west
$VAR1 = {
'box' => {
'n' => {
'data' => 'north'
},
'ne' => {
'data' => 'north-east'
},
'nw' => {
'data' => 'north-west'
}
}
};
Upvotes: 0