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

Reading External File

Started by TwelveEight, 22 February 2014 - 12:50 PM
TwelveEight #1
Posted 22 February 2014 - 01:50 PM
So, I'm working on making a functional rule board for my server, and I am wondering if it is possible to create an external text file so that basically I can add rules to the text file, and those rules will appear on a large monitor. Is there a way to do this?

Text File:
1. No blah blahing
2. Make sure to blah blah
3. Don't blah blah
4. Have fun! :D/>

Monitor:
–SERVER RULES–
1. No blah blahing
2. Make sure to blah blah
etc, etc.

Thank you for your time :)/>
TheOddByte #2
Posted 22 February 2014 - 03:31 PM
Well yeah ofcourse you can, First you should load the files content into a table like this.
( I'm lazy, I copied this from one of my APIs :P/> )


--[[ 
 @description    "Converts whole file into table"

 @param          filename,    string
 @return         table
--]]
function loadTable( filename )

    if fs.exists(filename) then
   local file = fs.open(filename,"r")
        local content = {}
        local line = file.readLine()
            repeat
                table.insert(content,line)
                line = file.readLine()
            until line == nil

 file.close()
 return content
else
   error("File doesn't exist: "..filename, 2)
end

end
Use that function to get the files' content stored in a table.
And now you should be able todo this

Example

local content = loadTable( "somefile" )
for i = 1, #content do
    print( content[i] )
end
TwelveEight #3
Posted 22 February 2014 - 04:09 PM
Well yeah ofcourse you can, First you should load the files content into a table like this….

I'm not really familiar with tables, sorry. How would I get this to then write itself to a monitor? I made a little thing that works fine on the Terminal, but not so much when it goes to the monitor and I can't use print. Sorry for being noobish. :(/>
Lyqyd #4
Posted 22 February 2014 - 04:59 PM
Moved to Ask a Pro.
TwelveEight #5
Posted 22 February 2014 - 05:04 PM
Okay, I think I figured it out.


What it looks like in game (pardon the mild language, it's an example text): http://i.imgur.com/Hq83Kzr.png

local m = peripheral.wrap("right")
function loadTable( filename )
	if fs.exists(filename) then
   local file = fs.open(filename,"r")
		local content = {}
		local line = file.readLine()
			repeat
				table.insert(content,line)
				line = file.readLine()
			until line == nil
file.close()
return content
else
   error("File doesn't exist: "..filename, 2)
end
end
function lineBreak()
local x, y = m.getCursorPos()
m.setCursorPos(1, y+1)
end
local content = loadTable( "disk/rules.txt" )
for i = 1, #content do
	m.write( content[i])
lineBreak()

end

However, now I need to be able to pick certain lines out of a file (in this case a blacklist in a cfg) so that while the file looks like:

Spoiler

Some Group:
- 6665464
- 8675309
- 31415
Blacklist:
- '30208:1:byp-prot '
- '25356:0:byp-prot '
- '9263:0:byp-prot '
- '2414:5:byp-prot '
- '407:0:byp-prot '
- '237:0:byp-prot '
- '7778:0:byp-prot '
Blacklist Placement: []
-464648684

etc.

The text on the monitor will only display anything in the Blacklist group, e.g.
Spoiler


- '30208:1:byp-prot '
- '25356:0:byp-prot '
- '9263:0:byp-prot '
- '2414:5:byp-prot '
- '407:0:byp-prot '
- '237:0:byp-prot '
- '7778:0:byp-prot '

The list needs to be able to be edited at any time without editing the code itself.
Edited on 22 February 2014 - 04:23 PM
Bomb Bloke #6
Posted 22 February 2014 - 08:52 PM
Currently, the table loader loops through the file dumping every line it reads into a table, until it reaches the end.

You'd want to split that into two loops: One which reads until it hits the line "Blacklist:" (without putting anything in the table), and one that reads until it hits the line "Blacklist Placement: []" (while putting stuff in the table).
Lyqyd #7
Posted 22 February 2014 - 08:59 PM
The alternative, of course, would be to dump the whole file into a table anyway, then look through the table and pick out the lines you want. It doesn't really matter what order you do it in, so whichever way is easier is going to be better in this case.
TwelveEight #8
Posted 22 February 2014 - 09:14 PM
Currently, the table loader loops through the file dumping every line it reads into a table, until it reaches the end.

You'd want to split that into two loops: One which reads until it hits the line "Blacklist:" (without putting anything in the table), and one that reads until it hits the line "Blacklist Placement: []" (while putting stuff in the table).

Could you possibly mock up a code example for me? I'm sorry, I'm not very skilled
Bomb Bloke #9
Posted 22 February 2014 - 09:30 PM
Well, the function you're currently using operates like this (this is the same code, but I've changed the formatting and added some comments):

function loadTable(filename)
	if fs.exists(filename) then
		local file = fs.open(filename,"r")
		local content = {}
		local line = file.readLine()          -- Read the first line from the file.
		
		repeat                                -- Loops the code inside...
			table.insert(content,line)         -- Puts the last line read into the table.
			line = file.readLine()             -- Reads another line from the file.
		until line == nil                     -- ... until we tried to read a non-existent line (the end of the file was reached).
		
		file.close()
		return content
	else
		error("File doesn't exist: "..filename, 2)
	end
end

So to split that one loop into two, you'd do something like:

local function loadTable(filename)
	if fs.exists(filename) then
		local file = fs.open(filename,"r")
		local content = {}
		local line
		
		repeat                                    -- Loops the below...
			line = file.readLine()                 -- Reads a line from the file.
		until line == "Blacklist:"                -- .. until we got the "Blacklist:" line.
		
		line = file.readLine()  -- Get the next line (some of the data we actually want).
		
		repeat                                    -- Loops the below...
			table.insert(content,line)             -- Puts the last line read into the table.
			line = file.readLine()                 -- Reads another line from the file.
		until line == "Blacklist Placement: []"   -- ... until we read past the desired block.
		
		return content
	else
		error("File doesn't exist: "..filename, 2)
	end
end