perl 使用for循环分别更新散列索引键

6kkfgxo0  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(162)

我有一个索引键名为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"

如何避免此更新?

l7wslrjt

l7wslrjt1#

my %hash_mpi;
$hash_mpi{value}->{bind_to} = "none";

my %hash;
$hash{value} = $hash_mpi{value};

@{$parallel_hash{"params_name_$i"}} = ("reporter","xml_obj",$hash{value});

是一种奇怪的方式

my $value = { bind_to => "none" };

$parallel_hash{ "params_name_$i" } = [ "reporter", "xml_obj", $value ];

你要给每个数组添加一个对相同哈希的引用。
你要

my %parallel_hash;
for my $i ( 0 .. 2 ) {
    $parallel_hash{ "function_name_$i" }  = "sharp_mpi_steps::run_mpirun";

    my %hash = (                              # Create a new hash.
       bind_to          => "none",
       reporter_message => "process_$i",
    );
    $parallel_hash{ "params_name_$i" } = [
       "reporter",
       "xml_obj",
       \%hash,                                # Create a reference to that hash.
    ];
}

{}同时执行这两项操作。

my %parallel_hash;
for my $i ( 0 .. 2 ) {
    $parallel_hash{ "function_name_$i" }  = "sharp_mpi_steps::run_mpirun";

    $parallel_hash{ "params_name_$i" } = [
       "reporter",
       "xml_obj",
       {
          bind_to          => "none",
          reporter_message => "process_$i",
       }
    ];
}

顺便说一句,这是一个非常奇怪的数据结构。使用数组会更有意义。

my @data;
for my $i ( 0 .. 2 ) {
    push @data, { 
       function => "sharp_mpi_steps::run_mpirun",
       params => [
          "reporter",
          "xml_obj",
          {
             bind_to          => "none",
             reporter_message => "process_$i",
          }
       ],
    };
}

相关问题