This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Get variables from a different file into a new one. Call them
Started by AnDwHaT5, 15 March 2013 - 10:27 AMPosted 15 March 2013 - 11:27 AM
What I want to do is make a huge list of variables and make a small code on a different program to call them when needed. I know there is a way but idk how to do it. I could use some help. lets say in one program called hi I have something = "test" and in a different program I want to call that variable somehow through programs and use it there.
Posted 15 March 2013 - 12:10 PM
Read the contents of the file.
What did we just do?
Well. we just read every line in the file that contains your variable(s). I won't go any further, so it will be up to you to index this new table and call the variable.
local handle = fs.open(fileName,"r")
local fileContents = {}
local lines = handle.readLine()
for lines in handle.lines do
table.insert(fileContents, lines)
end
What did we just do?
Well. we just read every line in the file that contains your variable(s). I won't go any further, so it will be up to you to index this new table and call the variable.
Posted 15 March 2013 - 12:22 PM
it says expected string also say the file is named hi and the line was at the first line and it said hello = "hi" and you want to use print(hello on the other program called l then how would that be done?
Posted 15 March 2013 - 12:27 PM
it says expected string also say the file is named hi and the line was at the first line and it said hello = "hi" and you want to use print(hello on the other program called l then how would that be done?
You can't just copy/paste code. Try to read it and see if there might be something you need to replace with your own code. In SuicidalSTDz' example, he has a variable fileName that you need to replace with the name of the file.
@Suicidal Sorry to tell ya mate but CC does not have a handle.lines iterator (useful as it would be). Here's an alternative AnDwHaT5:
local f = fs.open("hi", "r")
local content = {}
local line = f.readLine()
while line do
table.insert(content, line)
line = f.readLine()
end
print(content[1])
Edit: There are plenty of tutorials in the tutorial section on how to store variables in files. You should go read them. My personal favorite method would be textutils.serialize and textutils.unserialize. You can look those up on the wiki and imagine how they might be useful.
Posted 15 March 2013 - 12:32 PM
Bubba, I hate to break it to ya but, CC does have a built in lines iterator. I use it all the time <_</>
local handle = fs.open(fileName, "r")
local table = {}
local lines = handle.readLine()
for lines in handle.lines do
table.insert(table,lines)
end
Posted 15 March 2013 - 12:35 PM
now say you want to use it in a input like if user == content[1] then will that work?
Posted 15 March 2013 - 12:35 PM
Bubba, I hate to break it to ya but, CC does have a built in lines iterator. I use it all the time <_</>local handle = fs.open(fileName, "r") local table = {} local lines = handle.readLine() for lines in handle.lines do table.insert(table,lines) end
Err… I just tested this one out in CC-emu. Unless it's been added in 1.51.
Here's the program:
local f = fs.open('exampleFile', 'r')
for i,v in pairs(f) do
print(i..": "..v)
end
f.close()
And here's the output:
readLine: function
readAll: function
close: function
Unless I'm just too tired to think right now, that should output "lines: function"
Should also note that I did try your code and it failed to work.
Posted 15 March 2013 - 12:37 PM
I just checked the files and it works great thanks one last thing say i want to make a file with a user and pass is there a way to add it into the hi file and use hi as a config file
Posted 15 March 2013 - 12:39 PM
I thought CC always had the built in handle.readLine iterator. Of course you have to define the lines (local lines = handle.readLine()) I just know that it works when I do it. It might have been added in 1.51.
Which way did you use? Im curious if the handle.readLine iterator worked for you.I just checked the files and it works great thanks one last thing say i want to make a file with a user and pass is there a way to add it into the hi file and use hi as a config file
Posted 15 March 2013 - 12:41 PM
i used local handle = fs.open(fileName, "r")
local table = {}
local lines = handle.readLine()
for lines in handle.lines do
table.insert(table,lines)
end
local table = {}
local lines = handle.readLine()
for lines in handle.lines do
table.insert(table,lines)
end
Posted 15 March 2013 - 12:46 PM
Weird. I just tried it again: copied and pasted really (except for the filename of course). Maybe that's just a CC-emu thing?
Posted 15 March 2013 - 12:52 PM
I use it in CC Emu :D/> That is very strange… Immibis told be about it to begin with (Even I was doubtful at first) but it ended up working for me. ^_^/>
EDIT: Is there anything in the file you are reading?
EDIT: Is there anything in the file you are reading?
Posted 15 March 2013 - 12:55 PM
I solved the main issue i just need to know how to write something into a file to make like a user and pass config file. i can do it but i would use shell.run("config") then make them add it lol but that wont work. :P/>
Posted 15 March 2013 - 01:02 PM
print("Please input username: ")
local input = read()
local handle = io.open(fileName,"r") --I like to use the io library for one-line files
handle:write("username = "..input)
handle:close()
You can do the rest like always ^_^/>Posted 15 March 2013 - 01:11 PM
it dosnt work SuicidalSTDz
Posted 15 March 2013 - 01:14 PM
EDIT: Lol, replace
io.open(fileName,"r")
with io.open(fileName,"w")
Such a noob mistake of me ^_^/>Posted 15 March 2013 - 01:22 PM
… io:27:Expected string, String
Posted 15 March 2013 - 01:23 PM
I use it in CC Emu :D/> That is very strange… Immibis told be about it to begin with (Even I was doubtful at first) but it ended up working for me. ^_^/>
EDIT: Is there anything in the file you are reading?
Indeed there is. Quite a bit in the file in fact. I don't know what's up xD I know about handle.lines from a regular Lua environment but never really use it seeing as how I thought it wasn't in CC.
Edit: Just tried in 1.5 on a server. Didn't work. I don't know what the hell is going on here :/
Posted 15 March 2013 - 01:24 PM
hmm ahh i fixed it now the last thing i need to know. Is there a way to make it search the program for the input because when i input the pass how will it know where to look in there?
hmm hold on let me try something
hmm hold on let me try something
Posted 15 March 2013 - 01:25 PM
It is definately odd that it won't work. Did you define the variable 'lines'? Are you opening the file in 'w' mode instead of 'r' mode? A number of things could be wrong.I use it in CC Emu :D/> That is very strange… Immibis told be about it to begin with (Even I was doubtful at first) but it ended up working for me. ^_^/>
EDIT: Is there anything in the file you are reading?
Indeed there is. Quite a bit in the file in fact. I don't know what's up xD I know about handle.lines from a regular Lua environment but never really use it seeing as how I thought it wasn't in CC.
EDIT: Wait, you said you copied the code? That means you used the table name 'table'… I use that as an example. Using that name as a table (Just like strings) will override the table api and string api
Posted 15 March 2013 - 01:26 PM
haha now last thing how do i make a file on the computer with one command
Posted 15 March 2013 - 01:29 PM
i mean i know how to make a api file which i will use a input to make a file and the pass will go in there and if i do this right i will have a basic user and pass system
Posted 15 March 2013 - 01:30 PM
It is definately odd that it won't work. Did you define the variable 'lines'? Are you opening the file in 'w' mode instead of 'r' mode? A number of things could be wrong.I use it in CC Emu :D/> That is very strange… Immibis told be about it to begin with (Even I was doubtful at first) but it ended up working for me. ^_^/>
EDIT: Is there anything in the file you are reading?
Indeed there is. Quite a bit in the file in fact. I don't know what's up xD I know about handle.lines from a regular Lua environment but never really use it seeing as how I thought it wasn't in CC.
I pretty much copied the same methods that you used, although I did change the file name to one that exists. I checked the name a few times too, so that's not the issue.
Here's my code:
local f = fs.open("vector", "r")
local lines = f.readLine()
for lines in f.lines do
print(lines)
end
f.close()
Btw, the file "vector" has about 150 lines in it and f.readAll() works fine.
See anything wrong there?
Posted 15 March 2013 - 01:32 PM
I just made an edit to that last post. Read it… I ensure that is the problem
Posted 15 March 2013 - 01:40 PM
I just made an edit to that last post. Read it… I ensure that is the problem
Err… I'm not really a noob here ;)/> I do know better than to overwrite the table variable. I didn't do that in my code anyway. Not to mention overwriting the table var would have no effect on printing the lines.
Posted 15 March 2013 - 01:41 PM
Darn… Props for catching that. Most people wouldn't have noticed (Including me :)/> Nah, not really) Well, I don't know what else to say except maybe post the code, but then again, you said you just copied it so…
Posted 15 March 2013 - 01:43 PM
Darn… Props for catching that. Most people wouldn't have noticed (Including me :)/> Nah, not really) Well, I don't know what else to say except maybe post the code, but then again, you said you just copied it so…
Nope I didn't really just copy/paste (although I have said that a few times; my apologies). Read 3 posts up ;)/>
@AnDwHaT5 Sorry if I'm clogging your thread up. I just want to figure this out :)/>
Posted 15 March 2013 - 01:47 PM
That is strange… Last time I used the lines iterator it worked… Let me test a bit :)/>
Posted 15 March 2013 - 01:48 PM
That is strange… Last time I used the lines iterator it worked… Let me test a bit :)/>
It apparently worked for AnDwHaT5… This must be a CC-emu/CC 1.481 thing.
Posted 15 March 2013 - 01:49 PM
bringing the real question back downhaha now last thing how do i make a file on the computer with one command
yup no problemThat is strange… Last time I used the lines iterator it worked… Let me test a bit :)/>
It apparently worked for AnDwHaT5…
Posted 15 March 2013 - 01:50 PM
anyways if you guys know the answer post it here i gtg i will be on tmmo to see it .
Posted 15 March 2013 - 01:51 PM
I SEE THE PROBLEM!
You do this:
End Of Report:
-Problem solved!
-SuicidalSTDz needs some coffee!
You do this:
for lines in f.lines do
It should be:for lines in f.readLine do
I don't know how I didn't see that at first. ^_^/> That was actually my bad since I posted the original code… Wait! How did it work for you if it was wrong?End Of Report:
-Problem solved!
-SuicidalSTDz needs some coffee!
Posted 15 March 2013 - 01:52 PM
haha now last thing how do i make a file on the computer with one command
Yeah sorry about that mate. To create a new file all you have to do is open one in write mode (fs.open(fileName, "w"))
To make a directory use fs.makeDir("dir")
Posted 15 March 2013 - 01:54 PM
I SEE THE PROBLEM!
You do this:It should be:for lines in f.lines do
I don't know how I didn't see that at first. ^_^/>for lines in f.readLine do
End Of Report:
-Problem solved!
Okay… Now things are making more sense XD But looking back at both your code and and the code that AnDwHaT5 pasted the f.readLine does not exist there… How the hell did it work for him?
Posted 15 March 2013 - 01:56 PM
That's my question! He must've taken it upon himself to fix it? But? PARADOX!I SEE THE PROBLEM!
You do this:It should be:for lines in f.lines do
I don't know how I didn't see that at first. ^_^/>for lines in f.readLine do
End Of Report:
-Problem solved!
Okay… Now things are making more sense XD But looking back at both your code and and the code that AnDwHaT5 pasted the f.readLine does not exist there… How the hell did it work for him?
EDIT: Maybe I should lay off the Eggnog… Heh, like that's gonna happen ^_^/>
Posted 15 March 2013 - 02:00 PM
That's my question! He must've taken it upon himself to fix it? But? PARADOX!I SEE THE PROBLEM!
You do this:It should be:for lines in f.lines do
I don't know how I didn't see that at first. ^_^/>for lines in f.readLine do
End Of Report:
-Problem solved!
Okay… Now things are making more sense XD But looking back at both your code and and the code that AnDwHaT5 pasted the f.readLine does not exist there… How the hell did it work for him?
Yeah… Strange. Anywho, thanks for the debugging/giving me a new way to interact with files. It's been a while since I've needed to ask questions about Lua but I guess today is my day to derp XD
Posted 15 March 2013 - 02:05 PM
Hehe, no problem. It is definately a nice way of interacting with file contents. Also the only reason I use the fs library… I would use the io library's io:lines() function however last time I tried it, it didn't work exactly.That's my question! He must've taken it upon himself to fix it? But? PARADOX!I SEE THE PROBLEM!
You do this:It should be:for lines in f.lines do
I don't know how I didn't see that at first. ^_^/>for lines in f.readLine do
End Of Report:
-Problem solved!
Okay… Now things are making more sense XD But looking back at both your code and and the code that AnDwHaT5 pasted the f.readLine does not exist there… How the hell did it work for him?
Yeah… Strange. Anywho, thanks for the debugging/giving me a new way to interact with files. It's been a while since I've needed to ask questions about Lua but I guess today is my day to derp XD
Hmm, I guess I have to do my Calculus homework again now… :(/> Darn Clueless members not needing to ask questions