Netlogo:从字符串中读取需要字面值
Netlogo: Read-from-string expects literal value
以下触发 "expected literal value" 警告,我错过了什么?我希望返回值 1000。
let item-cost ["dia-cost"]
let item-cost first item-cost
print read-from-string item-cost
正如您在评论中所说,dia-cost
是一个您希望退出的值为 1000
的变量。问题的核心是 read-from-string
只读取类型数字、列表、字符串、布尔值或无人的字面值。那里的措辞有点令人困惑,正如它所说的那样,"Interprets the given string as if it had been typed in the Command Center",所以我可以看到它看起来像一个变量应该如何用 read-from-string
给出它的值,就像你在命令中心看到的那样,但变量不是那些有限类型的字面值。
幸运的是,有一个简单的替代方法,您可以使用 the runresult
primitive,它会将字符串解释为变量名并为您提供值:
to test
let dia-cost 1000
let item-cost ["dia-cost"]
set item-cost first item-cost
show runresult item-cost ; prints "1000"
end
以下触发 "expected literal value" 警告,我错过了什么?我希望返回值 1000。
let item-cost ["dia-cost"]
let item-cost first item-cost
print read-from-string item-cost
正如您在评论中所说,dia-cost
是一个您希望退出的值为 1000
的变量。问题的核心是 read-from-string
只读取类型数字、列表、字符串、布尔值或无人的字面值。那里的措辞有点令人困惑,正如它所说的那样,"Interprets the given string as if it had been typed in the Command Center",所以我可以看到它看起来像一个变量应该如何用 read-from-string
给出它的值,就像你在命令中心看到的那样,但变量不是那些有限类型的字面值。
幸运的是,有一个简单的替代方法,您可以使用 the runresult
primitive,它会将字符串解释为变量名并为您提供值:
to test
let dia-cost 1000
let item-cost ["dia-cost"]
set item-cost first item-cost
show runresult item-cost ; prints "1000"
end