0

我想在 Perl 中构建一个具有散列和变量的结构,然后创建一个散列,每个字段都将包含我创建的结构。像这样:

use Class::Struct;

struct exmpl => {hash=>'%' , val => '$'}; 
my %hash_of_structs;
$hash_of_structs { "one" } = exmpl -> new ();

现在 hash_of_structs 有一个带有“one”键的字段,其中包含 struct exmpl。我的问题是如何将新值推送到结构内的哈希中?

我想出了如何使用结构中的值:

$hash_of_structs { "one" } -> val ("1");
printf ( "The value is: %d\n",$hash_of_structs { "one" }-> val );

但它与结构中的哈希的工作方式不同。我试过了:

$hash_of_structs { "one" } => hash{"uno"}("1");

谢谢 :)

4

1 回答 1

1

使用以下语法。如果传递了一个哈希引用,旧的内容就会被遗忘,如果你提供两个参数,就会添加一个键值对。

$hash_of_structs{one}->hash({'A', 'a', 'B', 'b'});
$hash_of_structs{one}->hash('key', 'value');
于 2013-08-23T09:44:17.863 回答