95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 01:52 AM
Hi all,
Is there a way you can specify what line the FS API reads when say like reading through a config file or do i have to do stuff with tables?
Please help, i am fairly new to tables so dont really know how they work, yes i have read the Wiki Tut page :P/> Still dont understand it tho same with tArgs.
Any help i will be greatly thankfull for.
3057 posts
Location
United States of America
Posted 07 January 2016 - 02:05 AM
fs.readLine reads a single line from a file. It automatically increments the line being read each time it is called. You cannot specify which file being read. You do not have to use a table, although it is much easier to use one for most situations.
local file_lines = {} --#create a blank table
local file = fs.open( "file_name", "r" ) --#open the file in read mode
for line in file.readLine do --#file.readLine is a function which acts similar to pairs or ipairs, and therefor can be used like this
file_lines[ #file_lines + 1 ] = line --#set file_lines[ the number of items in file_lines + 1 ] equal to the current line
end
print( file_lines[ 1 ] ) --#prints the first line of the file
http://lua-users.org/wiki/TablesTutorial
Edited on 07 January 2016 - 01:06 AM
95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 02:27 AM
fs.readLine reads a single line from a file. It automatically increments the line being read each time it is called. You cannot specify which file being read. You do not have to use a table, although it is much easier to use one for most situations.
local file_lines = {} --#create a blank table
local file = fs.open( "file_name", "r" ) --#open the file in read mode
for line in file.readLine do --#file.readLine is a function which acts similar to pairs or ipairs, and therefor can be used like this
file_lines[ #file_lines + 1 ] = line --#set file_lines[ the number of items in file_lines + 1 ] equal to the current line
end
print( file_lines[ 1 ] ) --#prints the first line of the file
http://lua-users.org.../TablesTutorial
So the code there will basicly read the file and save it to a table so i can refer to it as line 1 would be file_line[1] 2nd line would be file_line[2] ?
I dont fully understand, sorry
3057 posts
Location
United States of America
Posted 07 January 2016 - 02:55 AM
Yes.
I commented each line as to what exactly it is doing, but the net result of it is to create a table where file_line[ 1 ] is line 1, etc.
95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 02:23 PM
Yes.
I commented each line as to what exactly it is doing, but the net result of it is to create a table where file_line[ 1 ] is line 1, etc.
Ok, so using that code
wirelessConfigFile = fs.open("Farm_Cfg/WirelessControl/config", "r")
for line in wirelessConfigFile.readLine do
configFileLine[ file_lines + 1 ] = line
end
print(configFileLine[1])
I get a error:Farm:107: attempt to perform arithmetic __add on nil and number
The tabel is listed at the top of the program
Edited on 07 January 2016 - 01:24 PM
7083 posts
Location
Tasmania (AU)
Posted 07 January 2016 - 02:30 PM
You likely never definded "file_lines".
Sticking a hash (#) in from of a value returns its length. In the case of a string, you get the number of characters. In the case of a numerically-indexed table, you get the number of elements in that table.
Yami originally used #file_lines to get the number of entries in his file_lines table. You renamed the table to configFileLine and must therefore use #configFileLine to get its length.
You also need to close that file handle - though truth be told it'd be easier to rig your loop to use io.lines(), which does it for you:
local configFileLine = {}
for line in io.lines("Farm_Cfg/WirelessControl/config") do
configFileLine[ #configFileLine + 1 ] = line
end
print(configFileLine[1])
95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 02:52 PM
You likely never definded "file_lines".
Sticking a hash (#) in from of a value returns its length. In the case of a string, you get the number of characters. In the case of a numerically-indexed table, you get the number of elements in that table.
Yami originally used #file_lines to get the number of entries in his file_lines table. You renamed the table to configFileLine and must therefore use #configFileLine to get its length.
You also need to close that file handle - though truth be told it'd be easier to rig your loop to use io.lines(), which does it for you:
local configFileLine = {}
for line in io.lines("Farm_Cfg/WirelessControl/config") do
configFileLine[ #configFileLine + 1 ] = line
end
print(configFileLine[1])
Thank you both, it works and i have been long trying to find something like this!
95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 05:49 PM
You likely never definded "file_lines".
Sticking a hash (#) in from of a value returns its length. In the case of a string, you get the number of characters. In the case of a numerically-indexed table, you get the number of elements in that table.
Yami originally used #file_lines to get the number of entries in his file_lines table. You renamed the table to configFileLine and must therefore use #configFileLine to get its length.
You also need to close that file handle - though truth be told it'd be easier to rig your loop to use io.lines(), which does it for you:
local configFileLine = {}
for line in io.lines("Farm_Cfg/WirelessControl/config") do
configFileLine[ #configFileLine + 1 ] = line
end
print(configFileLine[1])
Okay so now something weird is happening
function remoteConnect()
rednet.open("right")
ID, CMD = rednet.receive()
if ID == configWirelessFileLine[2] then
print(configWirelessFileLine[1])
else
print(configWirelessFileLine[2])
end
end
configWirelessFileLine[2] = 9 but when i send something from computer ID: 9 it runs the else statement? Anyone know why?
Edited on 07 January 2016 - 04:52 PM
3057 posts
Location
United States of America
Posted 07 January 2016 - 05:55 PM
Comparing a number and a string will always result in false. Anything in a file is a string, unless you tonumber it.
95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 06:05 PM
Comparing a number and a string will always result in false. Anything in a file is a string, unless you tonumber it.
How would i do that then? i have never had to convert somthing to number or string before :P/>
3057 posts
Location
United States of America
Posted 07 January 2016 - 06:19 PM
if ID == tonumber( configWirelessFileLine[ 2 ] ) then
95 posts
Location
A CPU some where in Bolton, UK
Posted 07 January 2016 - 06:24 PM
if ID == tonumber( configWirelessFileLine[ 2 ] ) then
Thank you so much xD