我目前正在尝试做的是制作一个包含 person_records (作为值)的电子邮件地址表(作为键)。person_record 在其中包含 6 个左右的东西。我遇到的问题是,当我尝试将电子邮件地址分配为表的键时,它会抱怨并说表索引为零......这是我到目前为止所拥有的:
random_record = split(line, ",")
person_record = {first_name = random_record[1], last_name = random_record[2], email_address = random_record[3], street_address = random_record[4], city = random_record[5], state = random_record[6]}
email_table[person_record.email_address] = person_record
我编写了自己的拆分函数,它基本上接受一行输入并提取 6 个逗号分隔值并将它们存储在一个表中(random_record)
当我尝试说 email_table[person_record.email_address] = person_record 时出现错误。但是当我打印出 person_record.email_address 它不是零时,它会打印出我存储在其中的字符串。我很困惑。
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end