This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
popdog15's profile picture

Check A File For Specific Text?

Started by popdog15, 22 August 2013 - 08:15 PM
popdog15 #1
Posted 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.
PixelToast #2
Posted 22 August 2013 - 11:05 PM
if you have a file like this for example:

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
popdog15 #3
Posted 22 August 2013 - 11:18 PM
if you have a file like this for example:

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
Though, that would just output it, right? I'm looking to check, rather than output.
PixelToast #4
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
popdog15 #5
Posted 23 August 2013 - 12:34 AM
isAdmin=line:match(".*"..user.." :(/>[^\n]+).")=="admin"
or if you just have a normal list with just names
isAdmin=line:match(".*("..user..")\n*")~=nil
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.
PixelToast #6
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 AM
Goof #7
Posted 23 August 2013 - 01:31 AM
EDIT: *got ninja'ed by PixelToast*
jay5476 #8
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") 
H4X0RZ #9
Posted 23 August 2013 - 11:44 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") 
If you don't specify a mode, it's automaticaly set to read.
theoriginalbit #10
Posted 23 August 2013 - 12:00 PM
If you don't specify a mode, it's automaticaly set to read.
Incorrect… you would get the following error
<filename>:<line number>: Expected string, string
H4X0RZ #11
Posted 23 August 2013 - 01:12 PM
If you don't specify a mode, it's automaticaly set to read.
Incorrect… you would get the following error
<filename>:<line number>: Expected string, string
okay, (I've checked the APIs) It's a feature of the IO API, the FS API don't provides this feature… right?
PixelToast #12
Posted 23 August 2013 - 01:23 PM
okay, (I've checked the APIs) It's a feature of the IO API, the FS API don't provides this feature… right?
yes

function open( _sPath, _sMode )
local sMode = _sMode or "r"
/me thinks of using the io api again
theoriginalbit #13
Posted 23 August 2013 - 01:24 PM
okay, (I've checked the APIs) It's a feature of the IO API, the FS API don't provides this feature… right?
Correct… If you do this code:

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()
Zudo #14
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?
PixelToast #15
Posted 23 August 2013 - 02:37 PM
Why not use have a table which has textutils.serialize applied to it and saved in a file?
this is easier, he can edit the file directly instead of using textutils.serialize
Cranium #16
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.