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

Tokenize string

Started by The_Puzzlemaker, 20 October 2016 - 11:34 PM
The_Puzzlemaker #1
Posted 21 October 2016 - 01:34 AM
I am making a configuration program for versions before the configuration API is implemented, and I want to take the lines of the file and turn it into a table where the configuration item's name is the key and the configuration item's value is the value in a table. How do I do this?

I want to seperate between the key and value with a colon and also seperate each string with a semicolon.
Edited on 21 October 2016 - 12:21 AM
Emma #2
Posted 21 October 2016 - 02:40 AM
One way to do this is to read each line, and for each line, find where the semicolon is, and take the first part and set the table's value at that key to be whatever is after the semicolon.

Something like this:


local config = {}
local handle = fs.open("config.cfg", "r")
local line = handle.readLine()
while line do
  local sep = line:find(":")
  config[line:sub(1, sep-1)] = line:sub(sep + 1)
  line = handle.readLine()
end
handle.close()
KingofGamesYami #3
Posted 21 October 2016 - 02:53 AM
By "configuration API" I assume you mean the settings API. If you are attempting to replicate it's functionality, you should read through it's code.