入力として受け取る次の文字列データがあります。
"route1,1234,1,no~,,route2,1234,1,no~,"
データの 2 つの「レコード」を表します...各レコードには 4 つのフィールドがあります。この文字列を個々の列/フィールドに解析するコードを作成しました。しかし、機能していない部分は、フィールド 2 に重複があるかどうかをテストするときです。フィールド 2 は、現在値として「1234」を持っているフィールドです。
コードは次のとおりです。
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
local check_for_duplicate_entries = function(route_data)
local route
local route_detail = {}
local result =true
local errtxt
local duplicate = false
print("received :" ..route_data)
route = string.gsub(route_data, "~,,", "~")
route = route:sub(1,string.len(route)-2)
print("route :" ..route)
-- break up in to an array
route = string.split(route,"~")
for key, value in pairs(route) do
route_detail[key] = string.split(value,",")
end
local list_of_second_column_only = {}
for key,value in pairs(route_detail) do
local temp = value[2]
print(temp .. " - is the value I'm checking for")
if list_of_second_column_only[temp] == nil then
print("i dont think it exists")
list_of_second_column_only[key] = value[2]
print(list_of_second_column_only[key])
else
--found a duplicate.
return true
end
end
return false
end
print(check_for_duplicate_entries("route1,1234,1,no~,,route2,1234,1,no~,"))
私が間違っているのはテストだと思います:
if list_of_second_column_only[temp] == nil then
temp に含まれる値の値ではなく、値 temp のキーをチェックしていると思います。しかし、構文を修正する方法がわかりません。また、これを行うためのより効率的な方法があるかどうか疑問に思っています。入力として受け取る「レコード」の数は動的/不明であり、各レコードの2番目の列の値も同様です。
ありがとう。
編集1
私が参考にしようとしていた投稿は次のとおりです: Lua リストで項目を検索する
答えでは、テーブル全体をループするのではなく、テーブル内のレコードを値でテストする方法を示しています...
if items["orange"] then
-- do something
end
私は遊んで、似たようなことをしようとしていました...