Jump to content




Check A File For Specific Text?


  • You cannot reply to this topic
15 replies to this topic

#1 popdog15

  • Members
  • 82 posts

Posted 22 August 2013 - 09: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.

#2 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 22 August 2013 - 10: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

#3 popdog15

  • Members
  • 82 posts

Posted 22 August 2013 - 10:18 PM

View PostPixelToast, on 22 August 2013 - 10:05 PM, said:

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.

#4 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 22 August 2013 - 10: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

#5 popdog15

  • Members
  • 82 posts

Posted 22 August 2013 - 11:34 PM

View PostPixelToast, on 22 August 2013 - 10:25 PM, said:

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.

#6 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 23 August 2013 - 12: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

#7 Goof

  • Members
  • 751 posts

Posted 23 August 2013 - 12:31 AM

EDIT: *got ninja'ed by PixelToast*

#8 jay5476

  • Members
  • 289 posts

Posted 23 August 2013 - 01: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") 


#9 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 23 August 2013 - 10:44 AM

View Postjay5476, on 23 August 2013 - 01:47 AM, said:

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.

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 August 2013 - 11:00 AM

View PostFreack100, on 23 August 2013 - 10:44 AM, said:

If you don't specify a mode, it's automaticaly set to read.
Incorrect... you would get the following error

Quote

<filename>:<line number>: Expected string, string


#11 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 23 August 2013 - 12:12 PM

View Posttheoriginalbit, on 23 August 2013 - 11:00 AM, said:

View PostFreack100, on 23 August 2013 - 10:44 AM, said:

If you don't specify a mode, it's automaticaly set to read.
Incorrect... you would get the following error

Quote

<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?

#12 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 23 August 2013 - 12:23 PM

View PostFreack100, on 23 August 2013 - 12:12 PM, said:

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

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 August 2013 - 12:24 PM

View PostFreack100, on 23 August 2013 - 12:12 PM, said:

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()


#14 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 23 August 2013 - 01:27 PM

Why not use have a table which has textutils.serialize applied to it and saved in a file?

#15 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 23 August 2013 - 01:37 PM

View PostZudoHackz, on 23 August 2013 - 01:27 PM, said:

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

#16 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 23 August 2013 - 02:14 PM

I'd highly recommend the Lua User's Wiki rather than the lua documentation. It reads much easier in my opinion.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users