"George", "Bob" => "George", -6ren">
gpt4 book ai didi

perl - 如何在 Perl 哈希表中存储多个值?

转载 作者:行者123 更新时间:2023-12-03 10:50:00 26 4
gpt4 key购买 nike

直到最近,我一直使用相同的键将多个值存储到不同的哈希中,如下所示:

%boss = (
"Allan" => "George",
"Bob" => "George",
"George" => "lisa" );

%status = (
"Allan" => "Contractor",
"Bob" => "Part-time",
"George" => "Full-time" );

然后我可以引用 $boss("Bob")$status("Bob")但是如果每个键都可以拥有很多属性,这会变得很笨拙,而且我不得不担心保持哈希同步。

有没有更好的方法在散列中存储多个值?我可以将值存储为
        "Bob" => "George:Part-time"

然后用split把字符串拆开,不过肯定有更优雅的方式。

最佳答案

这是标准方式,根据 perldoc perldsc .

~> more test.pl
%chums = ( "Allan" => {"Boss" => "George", "Status" => "Contractor"},
"Bob" => {"Boss" => "Peter", "Status" => "Part-time"} );

print $chums{"Allan"}{"Boss"}."\n";
print $chums{"Bob"}{"Boss"}."\n";
print $chums{"Bob"}{"Status"}."\n";
$chums{"Bob"}{"Wife"} = "Pam";
print $chums{"Bob"}{"Wife"}."\n";

~> perl test.pl
George
Peter
Part-time
Pam

关于perl - 如何在 Perl 哈希表中存储多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/190450/

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