如何向这个hash里添加键/值
如何向这个hash里添加键/值
先给出我的代码:
#!/usr/bin/perl -w
use Data::Dumper;
my $hash;
$hash = &createWork($hash, "test");
my $stateHash;
$stateHash = &createStateHash($stateHash, "first");
my $actionHash;
$actionHash = &createActionHash($actionHash,"firstAction","second");
$stateHash = &addAction($stateHash, $actionHash);
$hash = &addState($hash, $stateHash);
$stateHash = &createStateHash($stateHash, "first2");
$actionHash = &createActionHash($actionHash,"firstAction2","second2");
$stateHash = &addAction($stateHash, $actionHash);
$hash = &addState($hash, $stateHash);
print Dumper($hash);
sub createWork{
my ($hash, $type) = @_;
#print "$type\n";
$hash={'type'=>"$type"};
return $hash;
}
sub createStateHash{
my ($stateHash, $state) = @_;
$stateHash = {"$state"=>{}};
return $stateHash;
}
sub createActionHash{
my ($actionHash, $action, $next_state) = @_;
$actionHash={'name'=>"$action",'next_state'=>"$next_state"};
return $actionHash;
}
sub addWork{
my ($type) = @_;
#print "$type\n";
$hash={'type'=>"$type"};
}
sub addState{
my ($hash, $stateHash) = @_;
my @state = keys (%{$stateHash});
$hash->{'state'}= $stateHash;
return $hash;
}
sub addAction{
my ($stateHash, $actionHash) = @_;
my @state = keys (%{$stateHash});
$stateHash->{"$state[-1]"}->{'action'}=$actionHash;
return $stateHash;
}
输出来的结果是:
$VAR1 = {
'type' => 'test',
'state' => {
'first2' => {
'action' => {
'name' => 'firstAction2',
'next_state' => 'second2'
}
}
}
};
我期望的结果是:
$VAR1 = {
'type' => 'test',
'state' => {
'first' => {
'action' => {
'name' => 'firstAction',
'next_state' => 'second'
}
},
'first2' => {
'action' => {
'name' => 'firstAction2',
'next_state' => 'second2'
}
}
}
};
这样的hash结构,可以看到,后面添加进去的一组hash把前面的覆盖了。
我把程序改成这个样子:
#!/usr/bin/perl -w
use Data::Dumper;
my $hash;
$hash = &createWork($hash, "test");
my $stateHash;
$stateHash = &createStateHash($stateHash, "first");
my $actionHash;
$actionHash = &createActionHash($actionHash,"firstAction","second");
$stateHash = &addAction($stateHash, $actionHash);
$hash = &addState($hash, $stateHash);
$stateHash = &createStateHash($stateHash, "first2");
$actionHash = &createActionHash($actionHash,"firstAction2","second2");
$stateHash = &addAction($stateHash, $actionHash);
$hash = &addState($hash, $stateHash);
print Dumper($hash);
sub createWork{
my ($hash, $type) = @_;
#print "$type\n";
$hash={'type'=>"$type"};
return $hash;
}
sub createStateHash{
my ($stateHash, $state) = @_;
$stateHash = {"$state"=>{}};
return $stateHash;
}
sub createActionHash{
my ($actionHash, $action, $next_state) = @_;
$actionHash={'name'=>"$action",'next_state'=>"$next_state"};
return $actionHash;
}
sub addWork{
my ($type) = @_;
#print "$type\n";
$hash={'type'=>"$type"};
}
sub addState{
my ($hash, $stateHash) = @_;
my @state = keys (%{$stateHash});
$hash->{'state'}->{"$state[-1]"}= $stateHash;
return $hash;
}
sub addAction{
my ($stateHash, $actionHash) = @_;
my @state = keys (%{$stateHash});
$stateHash->{"$state[-1]"}->{'action'}=$actionHash;
return $stateHash;
}
输出却变成这个样子了:
$VAR1 = {
'type' => 'test',
'state' => {
'first' => {
'first' => {
'action' => {
'name' => 'firstAction',
'next_state' => 'second'
}
}
},
'first2' => {
'first2' => {
'action' => {
'name' => 'firstAction2',
'next_state' => 'second2'
}
}
}
}
};
多出来一层hash的引用。