gpt4 book ai didi

perl - 如何在Perl中创建哈希哈希?

转载 作者:行者123 更新时间:2023-12-04 09:43:42 25 4
gpt4 key购买 nike

基于我目前对Perl中哈希的理解,我希望这段代码可以打印“hello world”。相反,它不打印任何内容。

%a=();

%b=();
$b{str} = "hello";
$a{1}=%b;

$b=();
$b{str} = "world";
$a{2}=%b;

print "$a{1}{str} $a{2}{str}";

我假设哈希就像数组一样,那么为什么不能使哈希包含另一个哈希呢?

最佳答案

  • 您应该始终使用“use strict;”。在您的程序中。
  • 使用引用和匿名哈希。


  • use strict;use warnings;
    my %a;

    my %b;
    $b{str} = "hello";
    $a{1}={%b};

    %b=();
    $b{str} = "world";
    $a{2}={%b};

    print "$a{1}{str} $a{2}{str}";
    {%b}创建对哈希 %b副本的引用。您需要在此处复制,因为稍后您将其清空。

    关于perl - 如何在Perl中创建哈希哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2908620/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com