Работа с текстовым файлом

Shamanhcik

Известный
Автор темы
33
7
Имеется текстовый документ со строками, формата:
Код:
1 205000 1 1854.25 -1915.19 15.26 0
где первое значение - номер дома; четвертое, пятое и шестое - координаты по осям.
Каким образом, при вводе команды с указанием номера дома, можно получить координаты из текстового документа и установить метку на карту?
 
  • Нравится
Реакции: Ken Block

Shamanhcik

Известный
Автор темы
33
7
проходи по всем строчкам, сравнивай ид, если подходит - бери координаты и ставь метку
Каким образом можно сделать проверку строк в файле? Мне нужна именно эта фишка
 

trefa

Известный
Всефорумный модератор
2,097
1,233
Код не проверял, авторство остаётся за мной. При создании темы, написать меня разработчиком.
Lua:
f = io.open("test.txt")

home = {}

for line in f:lines() do
if line:find("^%d+ %d+ %d+ %d+ %d+ %d+ %d+$") then
home[line:match("^(%d+) ")] = split(line, '%s')
end
end

sampRegisterChatCommand("/go_home", function(param)
if tonumber(param) ~= nil then
if home[tonumber(param)] ~= nil then
if placeWaypoint(home[tonumber(param)][4], home[tonumber(param)][5], home[tonumber(param)][6]) then
sampAddChatMessage("Метка установлена.", 0xffcecece)
end
else
sampAddChatMessage("Дома с таким id не существует.", 0xffcecece)
end
else
sampAddChatMessage("Используй: /go_home [id дома]", 0xffcecece)
end
end)

function split(str, delim, plain)
local tokens, pos, plain = {}, 1, not (plain == false) --[[ delimiter is plain text by default ]]
repeat
local npos, epos = string.find(str, delim, pos, plain)
table.insert(tokens, string.sub(str, pos, npos and npos - 1))
pos = epos and epos + 1
until not pos
return tokens
end
 

Shamanhcik

Известный
Автор темы
33
7
Код не проверял, авторство остаётся за мной. При создании темы, написать меня разработчиком.
Lua:
f = io.open("test.txt")

home = {}

for line in f:lines() do
if line:find("^%d+ %d+ %d+ %d+ %d+ %d+ %d+$") then
home[line:match("^(%d+) ")] = split(line, '%s')
end
end

sampRegisterChatCommand("/go_home", function(param)
if tonumber(param) ~= nil then
if home[tonumber(param)] ~= nil then
if placeWaypoint(home[tonumber(param)][4], home[tonumber(param)][5], home[tonumber(param)][6]) then
sampAddChatMessage("Метка установлена.", 0xffcecece)
end
else
sampAddChatMessage("Дома с таким id не существует.", 0xffcecece)
end
else
sampAddChatMessage("Используй: /go_home [id дома]", 0xffcecece)
end
end)

function split(str, delim, plain)
local tokens, pos, plain = {}, 1, not (plain == false) --[[ delimiter is plain text by default ]]
repeat
local npos, epos = string.find(str, delim, pos, plain)
table.insert(tokens, string.sub(str, pos, npos and npos - 1))
pos = epos and epos + 1
until not pos
return tokens
end
По всем номерам выдает сценарий отсутствия дома:
Lua:
sampAddChatMessage("Дома с таким id не существует.", 0xffcecece)
 

trefa

Известный
Всефорумный модератор
2,097
1,233