如何替换Perl哈希键? [英] How to replace a Perl hash key?

查看:136
本文介绍了如何替换Perl哈希键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ data = {
'key1'= > {
'key2'=> 'value1'
},
'key3'=> {
'key4'=> {
'key5'=> 'value2'
}
},
};

现在,我可以如何用其他键名称'key6'替换'key5' ?我知道如何循环访问哈希&转储值,但我不知道如何替换键或值的地方。感谢您的帮助。

解决方案

删除运算符返回值删除。所以这个

  $ data-> {key3} {key4} {key6} =删除$ data-> {key3} {key4} {key5} 

可以满足您的要求。


Let us say if I have a hash like this:

$data = {
    'key1' => {
                'key2' => 'value1'
              },
    'key3' => {
                'key4' => {
                            'key5' => 'value2'
                          }
              },
  };

Now, how can I replace the the key 'key5' with some other key name say 'key6'? I know how to loop through the hash & dump the values but I don't know how to replace keys or values in place. Thanks for your help.

解决方案

The delete operator returns the value being deleted. So this

$data->{key3}{key4}{key6} = delete $data->{key3}{key4}{key5}

will do what you're looking for.

这篇关于如何替换Perl哈希键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆