【发布时间】:2021-10-21 17:35:09
【问题描述】:
我已经在互联网上搜索了指导,并且已经为此工作了好几天,但无法理解数据结构。我也有一个带有嵌套数组的多级哈希结构。它看起来像这样
{'Suggestion' => {
'Car' => {'Make' => ['Toyota'] },
'Bike' => 'Giant',
'Motorcycle' => {'Make'=> ['BMW']}
};
我可以将Toyota 和BMW 提取到带有{"Suggestion"}->{"Car"}->{"Make"} and {"Suggestion"}->{"Motorcycle"}->{"Make"} 的字符串变量中。
当我尝试使用{"Suggestion"}->{"Bike"} 提取Giant 时,它给了我一个作为字符串(HASH(9879)) 的HASH 引用。当我使用Dumper({"Suggestion"}->{"Bike"}) 时,它给了我$VAR = {'Bike' => undef};。那么它是一个带有键和 undef 值的 Hash 吗?
然后,如果我尝试使用%({"Suggestion"}->{'Bike'}) 来尊重它,它会给我一个错误:
Can't use string ("Bike") as a HASH ref while "strict refs" in use at (eval 7552). So this is telling me that I'm trying to use a string that I want as a hash ref?
我做错了什么?
【问题讨论】:
-
你的 hashref 在变量中吗?
-
没有。我在循环中使用它,如上所示,通过使用 join() 创建一个长字符串。
-
在您的代码中
print Dumper($var)并发布输出,其中$var包含Suggestion。注意:posted in 消息结构有不平衡的大括号。 -
那我完全不明白你为什么有一个 hashref。请显示更多代码。
-
您在上面显示的内容很好,并且工作正常。您的实际代码中一定有 something 不同吗?你能发布一个失败的最小可运行示例吗?