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

inserting tables

Started by pinksheep00, 26 March 2013 - 03:40 AM
pinksheep00 #1
Posted 26 March 2013 - 04:40 AM
How can I get a table from "another file" and store it in a table like a database?

say I have a program and in the first line of the program there's a table
how can I get that and store it in a table?

here's the code

local fileList = fs.list("")
local programList = {}
for i = 1, #fileList do
if not fs.isDir(fileList[i]) then
  j = string.sub(fs.getName(fileList[i]), (#fs.getName(fileList[i])-4), #fs.getName(fileList[i]))
  if j == ".prog" then
   --???
   table.insert(programList, ???)
  end
end
end
Engineer #2
Posted 26 March 2013 - 05:41 AM
You want to use

table.insert(programList, j)

Questions? Ask them! :)/>
Edited on 26 March 2013 - 04:45 AM
pinksheep00 #3
Posted 26 March 2013 - 05:44 AM
so… how can i get the table i want to insert from the other file?
Engineer #4
Posted 26 March 2013 - 05:46 AM
Updated post, was a bit rude to do the LUA reference manual thing.
pinksheep00 #5
Posted 26 March 2013 - 05:56 AM
Updated post, was a bit rude to do the LUA reference manual thing.
but…but… you didn't answer my question :(/>
Sammich Lord #6
Posted 26 March 2013 - 05:59 AM
There is a messy way you could use.

f = fs.open('pathToFile', "r")
local t = f.readAll()
loadstring(t)()
f.close()
That will add the table into memory. Messy way but it is simple.
Engineer #7
Posted 26 March 2013 - 06:02 AM
I prefer:

-- write
local file = fs.open("path", "w")
file.write(textutils.serialize(programList))
file.close()

-- read
local file = fs.open(""path","r")
local reader = file.readAll()
file.close()
local programList = textutils.unserialize(reader)

Sorry that I am the derp I am. I saw the title inserting tables and saw in the code table.insert commented out. Next time I should read the actual post
Sammich Lord #8
Posted 26 March 2013 - 06:09 AM
I prefer:

-- write
local file = fs.open("path", "w")
file.write(textutils.serialize(programList))
file.close()

-- read
local file = fs.open(""path","r")
local reader = file.readAll()
file.close()
local programList = textutils.unserialize(reader)
Well, if he already manually had the table written out then he should use mine. If he had it serialized before then he should use yours.
pinksheep00 #9
Posted 26 March 2013 - 06:33 AM
There is a messy way you could use.

f = fs.open('pathToFile', "r")
local t = f.readAll()
loadstring(t)()
f.close()
That will add the table into memory. Messy way but it is simple.
so i tried this and its still not working…


local fileList = fs.list("")
local programList = {}
for i = 1, #fileList do
if not fs.isDir(fileList[i]) then
  j = string.sub(fs.getName(fileList[i]), (#fs.getName(fileList[i])-4), #fs.getName(fileList[i]))
  if j == ".prog" then
   f = fs.open(fileList[i], "r")
   local t = f.readAll()
   loadstring(t)
   f.close()
   table.insert(programList, t)
  end
end
end
print(#programList)
print(programList[2].name)

when i print the number of tables it returns 3 but when I print a part of that table it returns nil. Am I doing something wrong here?

btw here's an example of the table i want to store in a table :/

{name = "Test Program", minX = 2, minY = 2, maxX = 20, maxY = 12}
pinksheep00 #10
Posted 26 March 2013 - 08:04 AM
anyone?
Sammich Lord #11
Posted 26 March 2013 - 08:10 AM
You should store it as this:

tableName = {data = "more data"}
That will make it put the data in the "tableName" table.
pinksheep00 #12
Posted 26 March 2013 - 08:21 AM
You should store it as this:

tableName = {data = "more data"}
That will make it put the data in the "tableName" table.
but what if i need to insert another data in the tableName will that replace the last data I placed?
Engineer #13
Posted 26 March 2013 - 08:33 AM

local test = {}
local test.name = "name"

-- Now the table is: { name = "name" }

And yes it will replace just like this example:

local number = 235
print(number)
number = 5
print(number)

--for tables (you should know this by now, just in case)
local test = { }
local test.name = "myname"
print(test[1])
test.name = "anothername"
print(test[1])

So all the code together: (my way)

local fileList = fs.list("/")
local programList = {}
for i = 1, #fileList do
	if not fs.isDir(fileList[i]) then
		j = string.sub(fs.getName(fileList[i]), (#fs.getName(fileList[i])-4), #fs.getName(fileList[i]))
		if j == ".prog" then
			local file = fs.open(fileList[i])
			local reader = file.readAll()
			file.close()
			local t = textutils.unserialize(reader) -- A serialized table is {[1]="name",[2]="anothername"}
			for x = 1, #t do
				table.insert(programList, t[x])
			end
		end
	end
end

Im not even sure what your goal is. But this how you get all the data from the table that you read in the empty table.
pinksheep00 #14
Posted 26 March 2013 - 09:09 AM
my goal is turn these 3 tables

{name = "TestProgram 1", xMin = 22, yMin = 2, xMax = 42, yMax = 10}


{name = "TestProgram 2", xMin = 23, yMin = 4, xMax = 49, yMax = 12}


{name = "TestProgram 3", xMin = 2, yMin = 2, xMax = 20, yMax = 10}

into this:

ProgramList = {
	{name = "TestProgram 1", xMin = 22, yMin = 2, xMax = 42, yMax = 10},
	{name = "TestProgram 2", xMin = 23, yMin = 4, xMax = 49, yMax = 12},
	{name = "TestProgram 3", xMin = 2, yMin = 2, xMax = 20, yMax = 10}
}

I'm making a program that checks every files that has the file extension ".prog" and save all the information about them
by getting all their first line which are tables and store them in one table "ProgramList". the whole program is already written all i need is to get the tables from the other files, now I tried code you've written and it gives me an error "serialize:1: table index expected, got nil"
remiX #15
Posted 26 March 2013 - 10:31 AM
Something like this?

local dir = '/' -- This is the directory of files with extention '.prog'
local dirList = fs.list( dir )
local programsList = {}

for i = 1, #dirList do
	if not dirList[i].isDir and dirList[i]:find( '.prog' ) and dirList[i]:sub( -5 ) == '.prog' then
		local f = fs.open( dir .. '/' .. dirList[i], 'r' )
		local t = f.readLine()
		f.close()
		table.insert( programsList, textutils.unserialize( t ) )
	end
end
Engineer #16
Posted 26 March 2013 - 10:33 AM
Okay, I solved it :)/>


local fileList = fs.list("")
local programList = {}
for i = 1, #fileList do
		if not fs.isDir(fileList[i]) then
				j = string.sub(fs.getName(fileList[i]), (#fs.getName(fileList[i])-4), #fs.getName(fileList[i]))
				if j == ".prog" then
						local file = fs.open("yourFile", "r")
						local reader = file.readLine()
						file.close()
						table.insert( programList, reader)
				end
		end
end

--To proof it :)/>
for _,v in ipairs(programList) do
	print(v)
end

Tested this with x.prog with your first table.

Edit: sneaky ninja man remiX!:P/>
pinksheep00 #17
Posted 26 March 2013 - 10:40 AM
Tested this with x.prog with your first table.

the main problem I'm getting is that when I print something like this

print(programList[1].name)
remiX #18
Posted 26 March 2013 - 10:48 AM
Tested this with x.prog with your first table.

the main problem I'm getting is that when I print something like this

print(programList[1].name)

Try mine, it works for me.

Add this to the end if you want to make sure:
local f = fs.open( 'asd', 'w' )
f.write( textutils.serialize( programsList ) )
f.close()

for i, v in pairs( programsList ) do
    print( v.name )
end

Spoiler
local dir = '/' -- This is the directory of files with extention '.prog'
local dirList = fs.list( dir )
local programsList = {}

for i = 1, #dirList do
	if not dirList[i].isDir and dirList[i]:find( '.prog' ) and dirList[i]:sub( -5 ) == '.prog' then
		local f = fs.open( dir .. '/' .. dirList[i], 'r' )
		local t = f.readLine()
		f.close()
		table.insert( programsList, textutils.unserialize( t ) )
	end
end

local f = fs.open( 'asd', 'w' )
f.write( textutils.serialize( programsList ) )
f.close()

for i, v in pairs( programsList ) do
	print( v.name )
end
pinksheep00 #19
Posted 26 March 2013 - 10:54 AM
it's finally working :D/> thank you so much for helping me guys! :lol:/>