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

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

问题描述

假设我有这样的哈希:

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

现在,我如何用其他键名替换键key5",比如key6"?

Now, how can I replace the the key 'key5' with some other key name, say 'key6'?

我知道如何遍历哈希并转储值,但我不知道如何替换键或值.

I know how to loop through the hash and dump the values, but I don't know how to replace keys or values in place.

推荐答案

delete 运算符返回被删除的值.所以这个

The delete operator returns the value being deleted. So this

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

会做你想要的.

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

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