直到最近,我已经使用相同的键将多个值存储到不同的哈希中,如下所示:
%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