if FirstName:GetValue() == ""
or table.HasValue( Blocked, string.lower(FirstName:GetValue())) then
-- Checks if the name contains any "bad" words, or nothing was typed in.
FirstNameCHECK = false
text4:SetText("Bad Name")
text4:SetColor(Color(255,0,0,255))
else
FirstNameCHECK = true
text4:SetText("Good Name")
text4:SetColor(Color(0,255,0,255))
end此代码当前检查字符串是否与表中的条目完全相同。
我如何能够更改这段代码,以便检查插入的字符串(FirstName变量)是否包含表中的一个条目?
发布于 2015-01-27 19:42:06
低效的解决方案是遍历表并对每个元素调用string.find。对于非常大的表,这种方法可能会变得非常慢,因为您必须检查每个元素,但是除非您处理的是非常大的数据集,否则这种方法可能会非常好。
一种更聪明的方法是使用嵌套的表,由子字符串索引。例如,您可以有一个索引为a到z的根表。你用单词的第一个字母索引到那个表中,你会得到另一个同样结构的表。这个你用第二个字母索引,以此类推,直到你在正在检查的字母上找不到更多的表格,或者你到达了单词的末尾。在后一种情况下,您可能需要在表中添加一个唯一条目,以指示您所查找的确切单词是否在表中(因为没有更多的字母,您需要以某种方式检查这一点)。
这种方法有几个缺点。构建表可能非常占用内存,执行检查很可能比对较小表的简单方法慢。此外,操作查找表也不是那么简单(例如,考虑从其中删除一个单词)。因此,这种结构只对执行查找非常有用,对于其他任务,您应该坚持使用普通的表。
例如,您可能希望在查找表中维护对真实数据表中条目的引用列表,该列表允许您从特定的前缀字符串中获取数据表中的所有匹配项。
发布于 2015-01-29 17:02:42
针对其他问题(多维表格)中的此类问题- Loop until find 2 specific values in a table?提出了一个解决方案。
function MultiTableSearch(input,value,case,index_check)
if (input and type(input) == 'table') then
if (type(value) == 'table' and value == input) then
return true;
end
for key,object in pairs(input) do
if (index_check) then
if (case and type(input)=='string' and type(key)=='string') then
if (value:lower() == key:lower()) then -- to avoid exit the loop
return true;
end
else
if (key == value) then
return true
elseif(type(object)=='table') then
return MultiTableSearch(object,value,case,index_check)
end
end
else
if (case and type(value)=='string' and type(object) == 'string') then
if (value:lower() == object:lower()) then
return true;
end
elseif(type(object)=='table') then
if (value == object) then
return true;
else
return MultiTableSearch(object,value,case)
end
else
if (object == value) then
return true;
end
end
end
end
end
return false;
end用这个来称呼
MultiTableSearch(blocked,FirstName:GetValue(),true) -- table,name,case(true to ignore case)https://stackoverflow.com/questions/28178404
复制相似问题