【发布时间】:2012-10-25 15:45:47
【问题描述】:
我有一个哈希值 (HoH),它是通过在 mysql 查询上使用 select_allhashref 得到的。
该格式非常适合我想要的,并且我通过类似的方式还有另外两个 HoH。
我想最终将这些 HoH 合并在一起,唯一的问题(我认为)是一个 HoH 的“子哈希”中有一个与另一个 HoH 中的名称相同的键。 例如
my $statement1 = "select id, name, age, height from school_db";
my $school_hashref = $dbh->selectall_hashref($statement1, 1);
my $statement2 = "select id, name, address, post_code from address_db";
my $address_hashref = $dbh->selectall_hashref($statement2, 1);
所以当我转储数据时,我会得到如下结果:
$VAR1 = {
'57494' => {
'name' => 'John Smith',
'age' => '9',
'height' => '120'
}
}
};
$VAR1 = {
'57494' => {
'name' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
}
}
};
(这是一个示例,因此使用不同的名称可能看起来不合逻辑,但我需要它:))
所以我想将'name' 重命名为'address_name' 等。这可能吗?我知道你能做到
$hashref->{$newkey} = delete $hashref->{$oldkey};
(编辑:这是我在网上找到的一个示例,但尚未测试。)
但我不知道如何表示 'id' 部分。有什么想法吗?
【问题讨论】:
标签: perl hash-of-hashes