散列引用 - 重用内部散列来存储和检索下一个值

时间:2016-04-25 13:58:29

标签: perl hashmap perl-data-structures

我是Perl编程的新手,必须处理Hash of Hashes。我试图重用innerHash变量本质上是动态的。而且我希望内部项目存储在outerHash中,我添加了多少。以下是我的测试程序片段。

use warnings;
sub testHash {  
  my %outerHash = ();

  my %innerHash1 = ();
  my %innerHash2 = ();

  $innerHash1{"key1"} = "value1";
  $innerHash1{"key2"} = "value2";
  $innerHash1{"key3"} = "value3";
  $outerHash{"Master1"} = \%innerHash1;

  $innerHash2{"key4"} = "value4";
  $innerHash2{"key5"} = "value5";
  $innerHash2{"key6"} = "value6";
  $outerHash{"Master2"} = \%innerHash2;

  #delete $innerHash1{$_};
  %innerHash1 = ();
  #undef %innerHash1;

  $innerHash1{"key7"} = "value7";
  $innerHash1{"key8"} = "value8";
  $innerHash1{"key9"} = "value9";
  $outerHash{"Master3"} = \%innerHash1;

  foreach $outerItem (keys %outerHash){
    print "\n$outerItem: ";
    foreach $innerItem (keys %{$outerHash{$outerItem}}){
        print "\t $innerItem = $outerHash{$outerItem}{$innerItem}";
    }
    print "\n-------------------------------------------------------";
  }
  print "\n";
}

testHash;

输出:

Master3:     key8 = value8   key7 = value7   key9 = value9
-----------------------------------------------------------------
Master2:     key5 = value5   key6 = value6   key4 = value4
-----------------------------------------------------------------
Master1:     key8 = value8   key7 = value7   key9 = value9
-----------------------------------------------------------------

我知道在打印项目时会更新innerHash1的参考。在outerHash中拥有所有正确元素的正确方法是什么?在实际的编程场景中,我不能提前声明 n 变量。

2 个答案:

答案 0 :(得分:0)

你可能是perl的新手,但你理解指针的概念不是吗?代码 <?php //PSU QUERY $sqlpsu="SELECT id, psu, price, psuWATT FROM PSU"; $result = mysql_query($sqlpsu); echo "<select name='psu'>"; while ($row = mysql_fetch_array($result)) { echo '<option value="'.$row["id"].'">'.$row["psu"]. " £" .$row["price"].'</option>'; $pricePsu = $row["price"]; } echo "</select>"; ?> 表示&#34; $outerHash{"Master1"} = \%innerHash1;被分配了指向 $outerHash{"Master1"}&#34;的指针。所以你所观察到的是正确和预期的。

如果您想继续重新创建%innerHash并将其添加到%outerHash,您需要执行以下两项操作之一:

  • 按值分配。 %innerHash1
  • 每个循环只将%innerHash添加到%outerHash一次。由于%innerHash在循环内重新声明,而不是在循环之外,因此每个循环都会超出范围,但由于perl的引用计数,其内容将保留在outerHash中。根据您的观点,这可以被认为是&#34;试图过于聪明且有潜在危险&#34;,但我认为这很好。

答案 1 :(得分:0)

至少在我看来,“正确”的方式是永远不会宣布任何中间“内部”。只需直接使用参考文献:

my %outerHash;
$outerHash{Master1} = { key1 => 'value1', key2 => 'value2', key3 => 'value3'};
$outerHash{Master2} = { key4 => 'value4', key5 => 'value5', key6 => 'value6'};
$outerHash{Master3} = { key7 => 'value7', key8 => 'value8', key9 => 'value9'};

# or....
my %outerHash2;
$outerHash2{Master1}{key1} = 'value1';
$outerHash2{Master1}{key2} = 'value2';
$outerHash2{Master1}{key3} = 'value3';
$outerHash2{Master2}{key4} = 'value4';
$outerHash2{Master2}{key5} = 'value5';
$outerHash2{Master2}{key6} = 'value6';
$outerHash2{Master3}{key7} = 'value7';
$outerHash2{Master3}{key8} = 'value8';
$outerHash2{Master3}{key9} = 'value9';

# or...
my %outerHash3 = (
    Master1 => {
        key1 => 'value1',
        key2 => 'value2',
        key3 => 'value3'
    }
    Master2 => {
        key4 => 'value4',
        key5 => 'value5',
        key6 => 'value6'
    }
    Master3 => {
        key7 => 'value7',
        key8 => 'value8',
        key9 => 'value9'
    }
);