This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Lua] Making a new text file
Started by kylergs, 18 November 2012 - 08:18 AMPosted 18 November 2012 - 09:18 AM
Is there a command to make a new text file in Lua?
Posted 18 November 2012 - 09:19 AM
What do you mean?
Posted 18 November 2012 - 09:20 AM
As in using the fs api to create a new file (for something like config)
Posted 18 November 2012 - 09:27 AM
Yes there is, see more here: CC Wiki - FS (API)
Basically, you assign fs.open("newfile", "w") to a variable.
Then you can use the variable to call functions which write things into the file, like so:
Basically, you assign fs.open("newfile", "w") to a variable.
Then you can use the variable to call functions which write things into the file, like so:
local file fs.open("newfile", "w")
file.writeLine("This is a line")
file.close() -- never forget to close a file!
Edited on 18 November 2012 - 08:31 AM
Posted 18 November 2012 - 09:27 AM
Edit: Ninja'd by Espen
Well considering that you already know about the fs api, you can go and look on the wiki to find out how to use it or find a tutorial somewhere on the forums (there are many). I imagine that users are getting rather tired of this question.
Well considering that you already know about the fs api, you can go and look on the wiki to find out how to use it or find a tutorial somewhere on the forums (there are many). I imagine that users are getting rather tired of this question.
Posted 18 November 2012 - 09:30 AM
I didn't even know there was a wiki, I was just using the in game help.
Posted 18 November 2012 - 09:33 AM
Also, there is no help that i can find to make a file (only a directory). However I know it is possible so…
Posted 18 November 2012 - 09:35 AM
Ok cool, I always thought you wouldn't be able to open a file if it wasn't already there,
Posted 18 November 2012 - 09:50 AM
Yep, if you try to open a file in write mode ("w") then it will either be created or overwritten if it already exists.Ok cool, I always thought you wouldn't be able to open a file if it wasn't already there,
And btw. I think double-posting in close succession is frowned upon and you even triple-posted.
You can edit your posts with "Edit" in the lower right of your posts.
No offense, just wanted to let you know in case you didn't already. :)/>/>
Posted 18 November 2012 - 09:52 AM
Ok sorry :/ BTW, is it possible to insert a key into a table. I've been experimenting quite a bit and I cant find a way to do it…
Posted 18 November 2012 - 09:54 AM
Ok sorry :/ BTW, is it possible to insert a key into a table. I've been experimenting quite a bit and I cant find a way to do it…
A key? Do you mean variable? If so then yes:
aTable = {value1="A value", value2="another value"}
print(aTable.value1) --will print "A value"
Posted 18 November 2012 - 09:57 AM
As in a key-value pair, so I can 'create' a dictionary.
Didn't see the second bit. I in the same way that table.insert() does it, but with key-value pairs.
Didn't see the second bit. I in the same way that table.insert() does it, but with key-value pairs.
Posted 18 November 2012 - 10:01 AM
As in a key-value pair, so I can 'create' a dictionary.
Yes, lua makes this very simple.
local tDefinitions = {hello="Hello is a greeting";
goodbye="Goodbye is a farewell";}
print(tDefinitions.hello) --Prints out the definition of hello
Using for loops you can make this into an incredibly useful tool.
for i,v in pairs(tDefinitions) do
print(i..": ".. v)
end
This will output
"hello: Hello is a greeting"
"goodbye: Goodbye is a farewell"
For more information use the lua wiki
Posted 18 November 2012 - 10:09 AM
Yes, I knew you could do that but I need to load the data for a fairly complex dictionary from a text file. So I need to be able to inset a part where I can set both the key and the value to imputed data.
(The other way of doing it would require quite bit more code…)
(The other way of doing it would require quite bit more code…)
Posted 18 November 2012 - 10:11 AM
That link doesn't point to the PIL though, but to the lua-users wiki.For more information use the lua PIL
The PIL is here: http://www.lua.org/pil/
As for dynamically creating a 'dictionary':
You can use tablename[ string_variable_with_key_name ], like so:
local firstnames = { "Michael", "Robert", "Bill" }
local lastnames = { "Thompson", "Mitchell", "Parker" }
local persons = {}
for i = 1, #firstnames do
persons[ firstnames[i] ] = lastnames[i]
end
for k, v in pairs( persons ) do
print( k.." -> "..v )
end
Mind you that in this particular example though, both tables must be of the same size.
Just wanted to point out that these two…
myTable.name = "Peter"
myTable[ "name" ] = "Peter"
… are equivalent and thus you can make use of this …local key = "name"
myTable[ key ] = "Peter" -- and 'key' can come from anywhere, a loop for example where you iterate through a list of keys like I did above
Cheers
Edit: Whoops, my edit was a bit late as it seems. Orwell already posted about myTable[ key ]. My bad.^^
Edited on 18 November 2012 - 09:27 AM
Posted 18 November 2012 - 10:15 AM
Yes, I knew you could do that but I need to load the data for a fairly complex dictionary from a text file. So I need to be able to inset a part where I can set both the key and the value to imputed data.
(The other way of doing it would require quite bit more code…)
I bet you didn't look at the PIL at all. It's all in there.
Back to spoon feeding:
tbl = {}
tbl['key1'] = "first value"
tbl['key2'] = "second value"
tbl['key3'] = "third value"
print( tbl['key2'] ) -- would print "second value"
Posted 18 November 2012 - 10:17 AM
Got it
Posted this before I saw orwell's
(Also, he didn't link to the PIL, just the lua- users wiki, it isn't there. When I looked at the PIL I found it)
tDefinitions = {}
tDefinitions["hello"] = "Hello is a greeting" --Will create the key "hello"
Posted this before I saw orwell's
(Also, he didn't link to the PIL, just the lua- users wiki, it isn't there. When I looked at the PIL I found it)
Posted 18 November 2012 - 10:20 AM
Edit: Wow, looked like I posted a fairly long post for something that was already solved :/ bummer
Ah importation. That requires a fair bit more work. I would need to know what your dictionary looked like to be able to accurately show you how, but here is an example:
Example dictionary file (saved as "dict.txt"):
Example code:
Spoiler
Yes, I knew you could do that but I need to load the data for a fairly complex dictionary from a text file. So I need to be able to inset a part where I can set both the key and the value to imputed data.
(The other way of doing it would require quite bit more code…)
Ah importation. That requires a fair bit more work. I would need to know what your dictionary looked like to be able to accurately show you how, but here is an example:
Example dictionary file (saved as "dict.txt"):
word1: definition of word1
word2: definition of word2
word3: definition of word3
Example code:
function returnFileAsTable(aFile) --Will return the file as a table
local f = fs.open(aFile,'r')
local aTable = {}
local aLine = f.readLine()
while aLine ~= nil do
table.insert(aTable, aLine)
aLine = f.readLine()
end
return aTable
end
function separateString(aString, sep) --Separates aString with the delimiter sep
for i=1, #aString do
x = string.sub(aString, i, i)
if x == sep then
return string.sub(aString, 1, i-1), string.sub(aString, i+1)
end
end
end
function iterate(aTable) --Iterates over the table returned by returnFileAsTable() and performs separateString on each value
local keyTable = {}
local definitionTable = {}
for i=1, #aTable do
key, def = separateString(aTable[i], ":")
table.insert(keyTable, key)
table.insert(definitionTable, def)
end
return keyTable, definitionTable
end
local fileTable = returnFileAsTable("dict.txt")
local words, defs = iterate(fileTable)
for i=1, #words do
print(words[i]..": "..defs[i])
end
Posted 18 November 2012 - 10:28 AM
Haha, don't worry yours really helped with some other stuff I was thinking about. You basically wrote half my program for me :)/>/>
Posted 18 November 2012 - 10:29 AM
Haha, don't worry yours really helped with some other stuff I was thinking about. You basically wrote half my program for me :)/>/>
Lol glad I could help. Just try not to copy/paste because you'll never learn anything that way :D/>/>
Posted 18 November 2012 - 10:58 AM
I know, I always like to make sure I understand everything that is happening if I ever use someone else's code.
(I also did give credit to you and changed a bit, you can simplify the seperateString by using string.find()
(I also did give credit to you and changed a bit, you can simplify the seperateString by using string.find()
function seperateString(aString, sep)
p = string.find(aString, sep)
return string.sub(aString, 1, p-1), string.sub(aString, p+1)
end
Posted 18 November 2012 - 11:51 AM
I know, I always like to make sure I understand everything that is happening if I ever use someone else's code.
(I also did give credit to you and changed a bit, you can simplify the seperateString by using string.find()function seperateString(aString, sep) p = string.find(aString, sep) return string.sub(aString, 1, p-1), string.sub(aString, p+1) end
Wow can't believe I didn't think to do that xD No need to give credit to me either: this is fairly generic code.
Posted 18 November 2012 - 12:24 PM
If you're still here, I'm trying to be able to get the numerical value of colour.whatever from a text file. The problem is it is read as a string, I can't find a way to convert the "colours.red" to the numerical value… The "tonumber()" function doesn't work
Posted 18 November 2012 - 01:11 PM
local num = colors['red']
Posted 18 November 2012 - 01:24 PM
If you have "colors.blue", then you just need to cut off "colors." by using, for example:
string.sub( yourColorString, 8 )
Posted 18 November 2012 - 01:25 PM
Thanks
In regard to cutting the string I just changed the format I was saving it in
In regard to cutting the string I just changed the format I was saving it in