Hash Perl将哈希添加到末尾的哈希中

Hash Perl将哈希添加到末尾的哈希中,hash,structure,hash-of-hashes,perl,Hash,Structure,Hash Of Hashes,Perl,我正在尝试将哈希添加到我的哈希中,如下所示: %funkce = ( "funkce1" => { "file" => "soubor1", "name" => "jmeno1", "varargs" => "args", "rettype" => "navrat", "params" => [ "typ", "typ2"

我正在尝试将哈希添加到我的哈希中,如下所示:

  %funkce = (
    "funkce1" => {
      "file" => "soubor1",
      "name" => "jmeno1",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
                "typ",
                "typ2"
            ]
    },
    "funkce2" => {
      "file" => "soubor2",
      "name" => "jmeno2",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
          "typ",
          "typ2"
      ]
    }
  );
  $delka = keys %funkce;
  $funkce{ "funkce" . ($delka + 1)} = {
      "file" => "soubor3",
      "name" => "jmeno3",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
          "typ",
          "typ2"
        ]
    };

但有一个问题。最后一个哈希是add as first in%函数,但我希望它是最后一个。我怎样才能修好它?我做得对吗?谢谢,哈希不能保证插入顺序。您要求它散列您的密钥,因此
x>yf(x)>f(y)

如果您想保证插入顺序,尽管我认为没有理由引入开销(即
领带
),但标准方法是使用

列表有结尾,而不是散列。哈希是从一组名称或ID到一组对象或值的数学映射。如果我们想到狗的名字,那么,尽管我们可以按字母顺序排列狗的名字,但实际上没有“第一条狗”

从你的表现来看

push( @funkce
    , { "file"    => "soubor1"
      , "name"    => "jmeno1"
      , "varargs" => "args"
      , "rettype" => "navrat"
      , "params"  => [ qw<typ typ2> ]
      });
push(@funkce
,{“文件”=>“soubor1”
,“name”=>“jmeno1”
,“varargs”=>“args”
,“rettype”=>“navrat”
,“参数”=>[qw]
});
它也能起到同样的作用。键入
$funkce{'funke2'}
比键入
$funkce[2]
$funkce{'funkce'.$i}
几乎没有什么好处 如果还要增加其他名称,那么应该以这种方式进行除法:
$funkce{'funkce'}[2]/$funkce{'superfunkce'}[2]


对名称的离散部分使用哈希,对数字使用数组,这是对数据进行编程的好方法
$funkce{'funkce'}[2]
每一位都是一个单数实体,就像
$funkce{'funkce2'}
一样

如果需要有序项,请使用数组;如果需要命名(无序)项,请使用散列。要获得接近有序散列的内容,您需要嵌套数组/散列,或者对散列进行排序,或者使用一些绑定类

嵌套

 @funkce = (
    { name => "funkce1",
      "file" => "soubor1",
      "name" => "jmeno1",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
                "typ",
                "typ2"
            ]
    },
    { name => "funkce2",
      "file" => "soubor2",
      "name" => "jmeno2",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
          "typ",
          "typ2"
      ]
    }
  );
 push @funkce, {
  name => "funkce3",
  "file" => "soubor3",
  "name" => "jmeno3",
  "varargs" => "args",
  "rettype" => "navrat",
  "params" => [
      "typ",
      "typ2"
    ]
};
%funkce = ( ... ); # as in OP

# when using
foreach my $item (sort keys %funkce) {
  # do something with $funkce{$item}
}
排序

 @funkce = (
    { name => "funkce1",
      "file" => "soubor1",
      "name" => "jmeno1",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
                "typ",
                "typ2"
            ]
    },
    { name => "funkce2",
      "file" => "soubor2",
      "name" => "jmeno2",
      "varargs" => "args",
      "rettype" => "navrat",
      "params" => [
          "typ",
          "typ2"
      ]
    }
  );
 push @funkce, {
  name => "funkce3",
  "file" => "soubor3",
  "name" => "jmeno3",
  "varargs" => "args",
  "rettype" => "navrat",
  "params" => [
      "typ",
      "typ2"
    ]
};
%funkce = ( ... ); # as in OP

# when using
foreach my $item (sort keys %funkce) {
  # do something with $funkce{$item}
}
平局


但正如阿克塞曼所说,你可能不需要/不想要这个。

@Bibo如果你真的需要按特定的顺序排列钥匙,你可以随时对它们进行排序。你什么时候开始使用这种样式了?:)没有什么可修的。散列表示一个数学集合。元素要么是集合,要么不是集合。有序元素称为序列,并没有其他数据结构,如数组或列表。“散列”本身就意味着“置乱”:钥匙通过一个类似绞肉机的“散列函数”输入,该函数产生一个数字,与任何顺序无关。该数字用于将索引计算到表中,在表中查找该键的值。@briandfoy,因为读取了PBP。实际上,Damien并不推荐逗号优先,他建议在每一对样式后使用逗号,但它似乎与运算符优先的外观一致,在Damien证明其可读性后,我勉强同意了这一点。当我开始把操作符放在第一位时,这条漂亮、干净的线路就吸引了我。请参阅,以获得IxHash的更快替代方案。