看看你的第一个和第二个例子之间的区别,我认为你的结构有点偏离。我认为这更符合您的要求:
{
DATE => date_time_value,
PARAMETERS => {
param_name1 => parameter_value1,
param_name2 => parameter_value2
}
}
这样,带有数据的结构可能如下所示:
{
DATE_TIME => "10/31/2031 12:00am",
PARAMETERS => {
COLOR => "red",
SIZE => "Really big",
NAME => "Herman",
}
}
通常,您会想到具有包含值的字段的对象。想想 SQL 表或电子表格的一行。您有带有标题的列和包含该值的行。
让我们选一个员工。他们有姓名、年龄、工作和电话号码:
{
NAME => "Bob Smith",
AGE => "None of your business",
JOB => "Making your life miserable",
PHONE => "555-1212"
}
与表不同,每个条目都可以包含其他结构。例如,人们通常有多个电话号码,我们可能希望将姓氏与名字分开存储:
{
NAME => {
FIRST => "Bob",
LAST => "Smith"
}
AGE => "None of your business",
JOB => "Making your life miserable"
PHONE => {
CELL => "555.1234",
WORK => "555.1212"
}
}
然后我们有同时拥有多部手机的人。例如,鲍勃有两部手机。在这种情况下,我们将使每个电话类型字段成为一个值数组:
{
NAME => {
FIRST => "Bob",
LAST => "Smith",
}
AGE => "None of your business",
JOB => "Making your life miserable"
PHONE => {
CELL => ["555.1234", "555.4321"]
WORK => ["555.1212"]
}
}
并初始化它:
my $person = {};
$person->{NAME}->{FIRST} = "Bob";
$person->{NAME}->{LAST} = "Smith";
$person->{AGE} = "None of your business";
$person->{JOB} = "Making your life miserable";
$person->{PHONE}->{CELL}->[0] = "555.1234";
$person->{PHONE}->{CELL}->[1] = "555.4321";
$person->{PHONE}->{WORK}->[0] = ""555.1212";