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

[help] File Reading

Started by pablo67340, 19 November 2012 - 07:53 AM
pablo67340 #1
Posted 19 November 2012 - 08:53 AM
ok, i have 2 computers (me and my friends) and we log players info on them. BUT every now and then we want to merge them so were both updated. BUT weve already merged them once (meaning both files have the same names now.) After we merged them, we both kept adding diffrent names. BUT now were in need of another merge,BUT if i were to merge them again it will duplicate the names we merged before (because both files have the same names from our first merge). PLEASE HELP i only want it to merge the new names we added
Orwell #2
Posted 19 November 2012 - 09:15 AM
This is about ComputerCraft computers, right?
pablo67340 #3
Posted 19 November 2012 - 09:41 AM
Yes
billysback #4
Posted 19 November 2012 - 10:00 AM
Then this is in the wrong section!

however, you can do this like so:

local function checkFor(table, check)
	for i=1,#table do
		if table[i] == check then return true end
	end
	return false
end

local function adv_merge(dirs, output)
	local lines = {}
	for i=1,#dirs do
		if fs.exists(dirs[i]) then
			local file = fs.open(dirs[i])
			local line = file.readLine()
			while line ~= nil do
				if not checkFor(lines, line) then
					lines[#lines + 1] = line
				end
				line = file.readLine()
			end
			file.close()
		end
	end
	if #lines > 0 then
		if fs.exists(output) then fs.delete(output) end
		local file = fs.open(output)
		for i=1,#lines do
			file.writeLine(lines[i])
		end
		file.close()
	end
end
and calling

adv_merge({"file1.txt", "file2.txt"}, "output.txt")
(for example)


(sorry about only giving code and no explanation, but I just can't really be bothered, very sorry!)
pablo67340 #5
Posted 19 November 2012 - 10:17 AM
Ok ill give it a try
pablo67340 #6
Posted 19 November 2012 - 10:33 AM
ok i have what you put, and adv_merge({"file1", "file2"}, "output") at the bottom, when i launch the program nothing happend