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

前端之家收集整理的这篇文章主要介绍了如何在Perl哈希表中存储多个值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
直到最近,我已经使用相同的键将多个值存储到不同的哈希中,如下所示:
%boss = (
    "Allan"  => "George","Bob"    => "George","George" => "lisa" );

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

然后我可以引用$ boss(“Bob”)和$ status(“Bob”),但是如果每个键都有很多属性,那么这样做会变得笨拙,我必须担心保持哈希值同步。

有没有更好的方法来存储多个值在哈希中?我可以将值存储为

"Bob" => "George:Part-time"

然后拆分弦,但必须有一个更优雅的方式。

解决方法

这是 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相关文章