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

Matching strings with entries in a user list file?

Started by Chaz, 20 January 2016 - 08:09 PM
Chaz #1
Posted 20 January 2016 - 09:09 PM
Okay, so I'm still learning about how tables and data work as far as using them in programs, so here's what I need help with:

I'm writing a keycard-based lock system, which is admittedly a bit messy and not terribly secure at the moment, but once I get the basics down I can try and make it more secure.

So, the idea right now is that the computer waits for a user to insert a floppy disk into a drive, and if the label on the disk matches a name recognized as either an "employee" or an override passphrase, it'll open the door.

Now, what I want to do is, instead of having the "employee" variable be a single string, I'd like the program to read from a text file that contains a list of employee names, and if the floppy's name matches any of those strings, it unlocks the door. All I can figure out right now is that this probably requires a table or some method of storing a number of variables, but I'm not quite sure how to match it.

While I'm on the subject, there is a secondary thing I need help with - I'm also working on an additional function that allows new people to register as employees - I've got the basics down for how that works, but I need to figure out how to write the user's name to the aforementioned list of employees.

As always, I appreciate the help!
Lyqyd #2
Posted 20 January 2016 - 10:07 PM
If you had a table of strings that you wanted to look through to see if an input string matches any of the entries, you might do something like this:


local matchFound = false

for _, name in ipairs(myStringTable) do
  if inputString == name then
    matchFound = true
    break
  end
end

This will iterate through the table, and check each name against the input string, stopping early if it finds a match. After the loop, matchFound will remain false if the input string did not match any of the names in the table, and will be true if it matched at least one name.