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

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

我有一个索引键名为0-2的%parallel_hash:

  1. %parallel_hash
  2. (function_name_0 => "sharp_mpi_steps::run_mpirun",
  3. params_name_0 => ("reporter","xml_obj",$hash{value}),
  4. function_name_1 => "sharp_mpi_steps::run_mpirun",
  5. params_name_1 => ("reporter","xml_obj",$hash{value}),
  6. (function_name_2 => "sharp_mpi_steps::run_mpirun",
  7. params_name_2 => ("reporter","xml_obj",$hash{value})

我想用一个索引文本更新$hash{value}的内容。对params_name_$i中的每个$hash{value},只需添加另一个键和值:报告程序消息=〉“进程$i”
我代码是:

  1. package delete;
  2. use strict;
  3. use warnings FATAL => 'all';
  4. my %parallel_hash;
  5. my %hash_mpi;
  6. $hash_mpi{value}->{bind_to} = "none";
  7. for (my $i=0;$i<=2;$i++)
  8. {
  9. my %hash;
  10. $hash{value} = $hash_mpi{value};
  11. $parallel_hash{"function_name_$i"} = "sharp_mpi_steps::run_mpirun";
  12. @{$parallel_hash{"params_name_$i"}} = ("reporter","xml_obj",$hash{value});
  13. $parallel_hash{"params_name_$i"}->[2]->{reporter_message} = "process_$i";
  14. }
  15. print ("Done");
  16. 1;

我想得到:

  1. %parallel_hash
  2. 'function_name_0' = "sharp_mpi_steps::run_mpirun"
  3. 'params_name_0' =
  4. [0] = "reporter"
  5. [1] = "xml_obj"
  6. [2] =
  7. 'bind_to' = "none"
  8. 'reporter_message" = "process_0"
  9. 'function_name_1' = "sharp_mpi_steps::run_mpirun"
  10. 'params_name_1' =
  11. [0] = "reporter"
  12. [1] = "xml_obj"
  13. [2] =
  14. 'bind_to' = "none"
  15. 'reporter_message" = "process_1"
  16. 'function_name_2' = "sharp_mpi_steps::run_mpirun"
  17. 'params_name_2' =
  18. [0] = "reporter"
  19. [1] = "xml_obj"
  20. [2] =
  21. 'bind_to' = "none"
  22. 'reporter_message" = "process_2"

问题是添加“reporter_message”键后,它也会更新所有以前的“params_name_$i”:

  1. %parallel_hash
  2. 'function_name_0' = "sharp_mpi_steps::run_mpirun"
  3. 'params_name_0' =
  4. [0] = "reporter"
  5. [1] = "xml_obj"
  6. [2] =
  7. 'bind_to' = "none"
  8. 'reporter_message" = "process_2"
  9. 'function_name_1' = "sharp_mpi_steps::run_mpirun"
  10. 'params_name_1' =
  11. [0] = "reporter"
  12. [1] = "xml_obj"
  13. [2] =
  14. 'bind_to' = "none"
  15. 'reporter_message" = "process_2"
  16. 'function_name_2' = "sharp_mpi_steps::run_mpirun"
  17. 'params_name_2' =
  18. [0] = "reporter"
  19. [1] = "xml_obj"
  20. [2] =
  21. 'bind_to' = "none"
  22. 'reporter_message" = "process_2"

如何避免此更新?

l7wslrjt

l7wslrjt1#

  1. my %hash_mpi;
  2. $hash_mpi{value}->{bind_to} = "none";
  3. my %hash;
  4. $hash{value} = $hash_mpi{value};
  5. @{$parallel_hash{"params_name_$i"}} = ("reporter","xml_obj",$hash{value});

是一种奇怪的方式

  1. my $value = { bind_to => "none" };
  2. $parallel_hash{ "params_name_$i" } = [ "reporter", "xml_obj", $value ];

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

  1. my %parallel_hash;
  2. for my $i ( 0 .. 2 ) {
  3. $parallel_hash{ "function_name_$i" } = "sharp_mpi_steps::run_mpirun";
  4. my %hash = ( # Create a new hash.
  5. bind_to => "none",
  6. reporter_message => "process_$i",
  7. );
  8. $parallel_hash{ "params_name_$i" } = [
  9. "reporter",
  10. "xml_obj",
  11. \%hash, # Create a reference to that hash.
  12. ];
  13. }

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

  1. my %parallel_hash;
  2. for my $i ( 0 .. 2 ) {
  3. $parallel_hash{ "function_name_$i" } = "sharp_mpi_steps::run_mpirun";
  4. $parallel_hash{ "params_name_$i" } = [
  5. "reporter",
  6. "xml_obj",
  7. {
  8. bind_to => "none",
  9. reporter_message => "process_$i",
  10. }
  11. ];
  12. }

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

  1. my @data;
  2. for my $i ( 0 .. 2 ) {
  3. push @data, {
  4. function => "sharp_mpi_steps::run_mpirun",
  5. params => [
  6. "reporter",
  7. "xml_obj",
  8. {
  9. bind_to => "none",
  10. reporter_message => "process_$i",
  11. }
  12. ],
  13. };
  14. }
展开查看全部

相关问题