This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Check A File For Specific Text?
Started by popdog15, 22 August 2013 - 08:15 PMPosted 22 August 2013 - 10:15 PM
I'm making a security system, and I have a file with the 'admins' of it. I need a way for the program to check the admin file if the person using the player detector is indeed an admin. I've heard I can use string.sub() to do this, but I'm unaware of the syntax and really how it works.
Posted 22 August 2013 - 11:05 PM
if you have a file like this for example:
rank=line:match(".*"..user..":([^\n]+).")
basically it looks to see if there is something after user..":"
(.+) will output anything, as long as the user exists
more info on patterns here
Mk352:banned
Cloudy:admin
PixelToast:awesome
you could use this to get the user's rank:rank=line:match(".*"..user..":([^\n]+).")
basically it looks to see if there is something after user..":"
(.+) will output anything, as long as the user exists
more info on patterns here
Posted 22 August 2013 - 11:18 PM
Though, that would just output it, right? I'm looking to check, rather than output.if you have a file like this for example:you could use this to get the user's rank:Mk352:banned Cloudy:admin PixelToast:awesome
rank=line:match("."..user..":(/>[^\n]+).")
basically it looks to see if there is something after user..":"
(.+) will output anything, as long as the user exists
more info on patterns here
Posted 22 August 2013 - 11:25 PM
isAdmin=line:match(".*"..user.." :([^\n]+).")=="admin"
or if you just have a normal list with just names
isAdmin=line:match(".*("..user..")\n*")~=nil
or if you just have a normal list with just names
isAdmin=line:match(".*("..user..")\n*")~=nil
Posted 23 August 2013 - 12:34 AM
Hate to bother you, but I'm still confused. I need to check this from another program; not the file with the 'admin list' in it.isAdmin=line:match(".*"..user.." :(/>[^\n]+).")=="admin"
or if you just have a normal list with just names
isAdmin=line:match(".*("..user..")\n*")~=nil
Posted 23 August 2013 - 01:28 AM
local file=fs.open("admins","r")
local admins=file.readAll()
file.close()
while true do
e,p=os.pullEvent("player")
if admins:match(".*("..p..")\n*") then
-- do stuff
end
end
EDIT: fixed, sorry it was 2 AMPosted 23 August 2013 - 01:31 AM
EDIT: *got ninja'ed by PixelToast*
Posted 23 August 2013 - 02:47 AM
uhhh pixel don't you need to specify a mode eg.("r","w","a") so I believe it should be
fs.open("admins", "r")
Posted 23 August 2013 - 11:44 AM
If you don't specify a mode, it's automaticaly set to read.uhhh pixel don't you need to specify a mode eg.("r","w","a") so I believe it should befs.open("admins", "r")
Posted 23 August 2013 - 12:00 PM
Incorrect… you would get the following errorIf you don't specify a mode, it's automaticaly set to read.
<filename>:<line number>: Expected string, string
Posted 23 August 2013 - 01:12 PM
okay, (I've checked the APIs) It's a feature of the IO API, the FS API don't provides this feature… right?Incorrect… you would get the following errorIf you don't specify a mode, it's automaticaly set to read.<filename>:<line number>: Expected string, string
Posted 23 August 2013 - 01:23 PM
yesokay, (I've checked the APIs) It's a feature of the IO API, the FS API don't provides this feature… right?
function open( _sPath, _sMode )
local sMode = _sMode or "r"
/me thinks of using the io api againPosted 23 August 2013 - 01:24 PM
Correct… If you do this code:okay, (I've checked the APIs) It's a feature of the IO API, the FS API don't provides this feature… right?
local h = io.open("test")
for k,v in pairs(h) do
print(k," = ",v)
end
h:close()
it will work… this won't work…
local h = fs.open("test")
for k,v in pairs(h) do
print(k," = ",v)
end
h.close()
Posted 23 August 2013 - 02:27 PM
Why not use have a table which has textutils.serialize applied to it and saved in a file?
Posted 23 August 2013 - 02:37 PM
this is easier, he can edit the file directly instead of using textutils.serializeWhy not use have a table which has textutils.serialize applied to it and saved in a file?
Posted 23 August 2013 - 03:14 PM
I'd highly recommend the Lua User's Wiki rather than the lua documentation. It reads much easier in my opinion.