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

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

问题描述

让我们说一下我是否有这样的哈希值:

Let us say if I have a hash like this:

$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天全站免登陆