我有一个索引键名为0-2的%parallel_hash:
%parallel_hash
(function_name_0 => "sharp_mpi_steps::run_mpirun",
params_name_0 => ("reporter","xml_obj",$hash{value}),
function_name_1 => "sharp_mpi_steps::run_mpirun",
params_name_1 => ("reporter","xml_obj",$hash{value}),
(function_name_2 => "sharp_mpi_steps::run_mpirun",
params_name_2 => ("reporter","xml_obj",$hash{value})
我想用一个索引文本更新$hash{value}的内容。对params_name_$i中的每个$hash{value},只需添加另一个键和值:报告程序消息=〉“进程$i”
我代码是:
package delete;
use strict;
use warnings FATAL => 'all';
my %parallel_hash;
my %hash_mpi;
$hash_mpi{value}->{bind_to} = "none";
for (my $i=0;$i<=2;$i++)
{
my %hash;
$hash{value} = $hash_mpi{value};
$parallel_hash{"function_name_$i"} = "sharp_mpi_steps::run_mpirun";
@{$parallel_hash{"params_name_$i"}} = ("reporter","xml_obj",$hash{value});
$parallel_hash{"params_name_$i"}->[2]->{reporter_message} = "process_$i";
}
print ("Done");
1;
我想得到:
%parallel_hash
'function_name_0' = "sharp_mpi_steps::run_mpirun"
'params_name_0' =
[0] = "reporter"
[1] = "xml_obj"
[2] =
'bind_to' = "none"
'reporter_message" = "process_0"
'function_name_1' = "sharp_mpi_steps::run_mpirun"
'params_name_1' =
[0] = "reporter"
[1] = "xml_obj"
[2] =
'bind_to' = "none"
'reporter_message" = "process_1"
'function_name_2' = "sharp_mpi_steps::run_mpirun"
'params_name_2' =
[0] = "reporter"
[1] = "xml_obj"
[2] =
'bind_to' = "none"
'reporter_message" = "process_2"
问题是添加“reporter_message”键后,它也会更新所有以前的“params_name_$i”:
%parallel_hash
'function_name_0' = "sharp_mpi_steps::run_mpirun"
'params_name_0' =
[0] = "reporter"
[1] = "xml_obj"
[2] =
'bind_to' = "none"
'reporter_message" = "process_2"
'function_name_1' = "sharp_mpi_steps::run_mpirun"
'params_name_1' =
[0] = "reporter"
[1] = "xml_obj"
[2] =
'bind_to' = "none"
'reporter_message" = "process_2"
'function_name_2' = "sharp_mpi_steps::run_mpirun"
'params_name_2' =
[0] = "reporter"
[1] = "xml_obj"
[2] =
'bind_to' = "none"
'reporter_message" = "process_2"
如何避免此更新?
1条答案
按热度按时间l7wslrjt1#
是一种奇怪的方式
你要给每个数组添加一个对相同哈希的引用。
你要
{}
同时执行这两项操作。顺便说一句,这是一个非常奇怪的数据结构。使用数组会更有意义。