【问题标题】:Perl add Hash to Hash of Hashes at the endPerl 在最后添加 Hash 到 Hash of Hash
【发布时间】:2012-04-19 14:22:36
【问题描述】:

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

  %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"
        ]
    };

但是有一个问题。最后一个哈希在 %function 中添加为第一个,但我希望它作为最后一个。我该如何解决?我做得对吗?谢谢

【问题讨论】:

    标签: hash structure hash-of-hashes perl


    【解决方案1】:

    哈希不保证插入顺序。你要求它散列你的密钥,所以x > y <=/=> f(x) > f(y)

    如果你想保证插入顺序,虽然我认为没有理由引入开销(tie),但标准方法是使用Tie::IxHash

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

    从你的表现来看,

    push( @funkce
        , { "file"    => "soubor1"
          , "name"    => "jmeno1"
          , "varargs" => "args"
          , "rettype" => "navrat"
          , "params"  => [ qw<typ typ2> ]
          });
    

    效果一样好。输入$funkce{'funcke2'} 而不是$funkce[2]$funkce{ '$funkce' . $i } 而不是$funkce[$i] 几乎没有什么好处 如果您要增加其他名称,那么您应该以这种方式进行划分:$funkce{'funkce'}[2] // $funkce{'superfunkce'}[2]

    对名称的离散部分使用散列,对数字使用数组是编程数据的好方法。 $funkce{'funkce'}[2]$funkce{'funkce2'} 一样,都是一个单一的实体。

    【讨论】:

    • @Bibo 如果您确实需要按特定顺序排列键,您可以随时对它们进行排序。
    • 你从什么时候开始使用这种风格的? :)
    • 没有什么可修复的。哈希表示一个数学集合。一个元素要么是一个集合,要么不是。有序元素称为序列,还有其他数据结构,如数组或列表。 “散列”本身就是“打乱”的意思:关键是通过一个类似于绞肉机的“散列函数”,它产生一个数字,与任何顺序无关。该数字用于计算表中的索引,以查找该键的值。
    • @briandfoy,自从阅读 PBP。实际上,逗号优先不是 Damien 推荐的,他建议使用 comma-after-every-pair 风格,但它似乎是运算符优先的外观,在 Damien 展示其可读性后我不情愿地同意了。一旦我开始将操作员放在首位,这条漂亮、简洁的线条就吸引了我。
    • 查看 Tie::Hash::Indexed 以获得更快的 IxHash 替代方案。
    【解决方案2】:

    如果您需要有序项目使用数组,如果您想要命名(无序)项目使用哈希。要获得接近有序散列的东西,您需要嵌套数组/散列或对散列进行排序或使用一些绑定类。

    嵌套

     @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}
    }
    

    并列

    请参阅 Tie::IxHash,但正如 Axeman 所说,您可能不需要/不想要这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多