为什么 table.remove 和成对函数在 Roblox Studio 中都不起作用?
why is table.remove and in pairs function both not working in Roblox Studio?
我有以下代码,Roblox 开发人员和 Lua.org 手册都说应该可以从 table 中删除一个实例,这样我就可以存储为本地,但本地只持有零值。
table 在那里。它显示在打印功能上。它只是不会存储在应用程序中有用。
我已经尝试了此代码的多个版本,包括只使用 pairs 函数,只使用 table.remove 函数,以及使用和不使用 table 删除的位置,它都会生成无变量。
response = HttpService:GetAsync(mining)
data = HttpService:JSONDecode(response, Enum.HttpContentType.ApplicationJson)
local function tprint(t)
for k,v in pairs(t) do print(k,v) end
end
tprint(data)
local a = table.remove(data, 4)
local b = table.remove(data, 3)
local c = table.remove(data, 2)
local d = table.remove(data, 1)
解决方案如此简单,却又如此深刻。我现在可以将其用于 link 加密货币、银行账户、信用卡以及我想要的任何其他东西,直接进入 Roblox 或任何其他基于 lua 的程序。
a = (data["result"]["amount"])
在深入了解您看到的错误之前,请先了解一些背景信息。
Lua tables 有两种索引值的方法:数字和按键。很多时候你会看到这两种不同的方法被用来描述使用它的数据结构的种类。
数组和列表 table 使用数字键来索引信息。
local arr = {}
arr[1] = "abc"
arr[2] = 123
arr[3] = true
-- print the length of the array
print(#arr) -- 3
-- print the contents of the array
for i, v in ipairs(arr) do
print(i, v)
-- 1 abc
-- 2 123
-- 3 true
end
另一方面,字典和哈希映射以及关联数组使用键来存储信息:
local dict = {}
dict["foo"] = "abc"
dict["bar"] = 123
dict["blah"] = true
dict["katz"] = { 1, 2, 3 }
-- print the number of numerical keys in the dictionary
print(#dict) -- 0
-- print the contents of the dictionary
for k, v in pairs(dict) do
print(k, v)
-- foo abc
-- bar 123
-- blah true
-- katz table
end
虽然 lua 允许 table 同时使用这两种索引方法,但重要的是永远不要混合使用这两种方法,因为当您这样做时,行为会变得非常古怪。当 table 有键时,将其视为字典。当 table 具有数字索引时,将其视为数组。
当您使用 HttpService 将 JSON 字符串解码为 table 时,它会生成一个反映原始数据层次结构的字典。
您使用 table.insert()
和 table.remove()
调用的 table 库期望您正在使用的 table 是一个数组。
当你的数据这样排列时:
local data = {}
data["Success"] = true
data["StatusCode"] = 200
data["StatusMessage"] = "Success"
data["Headers"] = {} -- a dictionary of headers
data["Body"] = {
result = {
amount = 1,
depositAddress = "blah",
},
} -- after HttpService:JSONDecode() is called...
然后你告诉它删除带有 table.remove(data, 4)
的编号索引,它不会工作,因为没有数据存储在索引号 4 处。data
是字典,不是数组。
通常,尝试打印出具有多层数据的 table 的内容很烦人,尤其是 JSON tables,因为 pairs
函数一次只会索引一个级别。值得庆幸的是,Roblox 的 print
功能和输出小部件足够智能,可以为您完成这项工作。您可以简单地 print(data)
它会在输出中显示完整的 table 并允许您检查每个级别。
然后,一旦您了解了数据的结构,您就可以逐个值地遍历它。
local amount = data["Body"]["result"]["amount"]
-- or
local amount = data.Body.result.amount
我有以下代码,Roblox 开发人员和 Lua.org 手册都说应该可以从 table 中删除一个实例,这样我就可以存储为本地,但本地只持有零值。
table 在那里。它显示在打印功能上。它只是不会存储在应用程序中有用。
我已经尝试了此代码的多个版本,包括只使用 pairs 函数,只使用 table.remove 函数,以及使用和不使用 table 删除的位置,它都会生成无变量。
response = HttpService:GetAsync(mining)
data = HttpService:JSONDecode(response, Enum.HttpContentType.ApplicationJson)
local function tprint(t)
for k,v in pairs(t) do print(k,v) end
end
tprint(data)
local a = table.remove(data, 4)
local b = table.remove(data, 3)
local c = table.remove(data, 2)
local d = table.remove(data, 1)
解决方案如此简单,却又如此深刻。我现在可以将其用于 link 加密货币、银行账户、信用卡以及我想要的任何其他东西,直接进入 Roblox 或任何其他基于 lua 的程序。
a = (data["result"]["amount"])
在深入了解您看到的错误之前,请先了解一些背景信息。
Lua tables 有两种索引值的方法:数字和按键。很多时候你会看到这两种不同的方法被用来描述使用它的数据结构的种类。
数组和列表 table 使用数字键来索引信息。
local arr = {}
arr[1] = "abc"
arr[2] = 123
arr[3] = true
-- print the length of the array
print(#arr) -- 3
-- print the contents of the array
for i, v in ipairs(arr) do
print(i, v)
-- 1 abc
-- 2 123
-- 3 true
end
另一方面,字典和哈希映射以及关联数组使用键来存储信息:
local dict = {}
dict["foo"] = "abc"
dict["bar"] = 123
dict["blah"] = true
dict["katz"] = { 1, 2, 3 }
-- print the number of numerical keys in the dictionary
print(#dict) -- 0
-- print the contents of the dictionary
for k, v in pairs(dict) do
print(k, v)
-- foo abc
-- bar 123
-- blah true
-- katz table
end
虽然 lua 允许 table 同时使用这两种索引方法,但重要的是永远不要混合使用这两种方法,因为当您这样做时,行为会变得非常古怪。当 table 有键时,将其视为字典。当 table 具有数字索引时,将其视为数组。
当您使用 HttpService 将 JSON 字符串解码为 table 时,它会生成一个反映原始数据层次结构的字典。
您使用 table.insert()
和 table.remove()
调用的 table 库期望您正在使用的 table 是一个数组。
当你的数据这样排列时:
local data = {}
data["Success"] = true
data["StatusCode"] = 200
data["StatusMessage"] = "Success"
data["Headers"] = {} -- a dictionary of headers
data["Body"] = {
result = {
amount = 1,
depositAddress = "blah",
},
} -- after HttpService:JSONDecode() is called...
然后你告诉它删除带有 table.remove(data, 4)
的编号索引,它不会工作,因为没有数据存储在索引号 4 处。data
是字典,不是数组。
通常,尝试打印出具有多层数据的 table 的内容很烦人,尤其是 JSON tables,因为 pairs
函数一次只会索引一个级别。值得庆幸的是,Roblox 的 print
功能和输出小部件足够智能,可以为您完成这项工作。您可以简单地 print(data)
它会在输出中显示完整的 table 并允许您检查每个级别。
然后,一旦您了解了数据的结构,您就可以逐个值地遍历它。
local amount = data["Body"]["result"]["amount"]
-- or
local amount = data.Body.result.amount