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

Read and Modifiying a Table in a File

Started by Hayden_Almeida, 31 August 2015 - 08:06 PM
Hayden_Almeida #1
Posted 31 August 2015 - 10:06 PM
Hello guys. I Have one file called
"lista"

inside i have:

{
  Nome = "Thales",
  Cidade = "MG",
  Idade = "22",
}
{
  Nome = "Tio Cocota",
  Cidade = "MG",
  Idade = "55",
}

In another program i have:

  local file = fs.open("lista","r")
  local data = file.readAll()
  file.close()
  local a = textutils.unserialize(data)
  print("Nome: "..a.Nome)
  print("Idade: "..a.Idade)
  print("Cidade: "..a.Cidade)

Before when i only have the First table:

{
  Nome = "Thales",
  Cidade = "MG",
  Idade = "22",
}
it worked, but now i have "2 tables" inside the first File, the code gives an error, because i have 2 "Nome", first called "Thales" and the second "Tio cocota"

How can i read the first table separate of the second?
And how can i modify using the another program to modify this values in the file called "lista" ?
Edited on 31 August 2015 - 08:08 PM
Exerro #2
Posted 31 August 2015 - 11:22 PM
Well, the easiest way would be to do this in the file I'd say:

{
   {
	   -- table one contents
   },
   {
	   -- table two contents
   }
}
loading it like you do now, and indexing it like t[1] for the first person, t[2] for the second…

Alternatively, you could use gsub() to replace every } with };, and before trying to unserialize it, add a { and } to the start and finish, making it effectively like the example above but allowing you to store it like you do currently. This should do it:

local function loadstr( str )
	return textutils.unserialize( "{" .. str:gsub( "{", "{;" ) .. "}" )
end
Edited on 31 August 2015 - 09:23 PM
Hayden_Almeida #3
Posted 01 September 2015 - 12:15 AM
Well, the easiest way would be to do this in the file I'd say:

{
   {
	   -- table one contents
   },
   {
	   -- table two contents
   }
}
loading it like you do now, and indexing it like t[1] for the first person, t[2] for the second…

Alternatively, you could use gsub() to replace every } with };, and before trying to unserialize it, add a { and } to the start and finish, making it effectively like the example above but allowing you to store it like you do currently. This should do it:

local function loadstr( str )
	return textutils.unserialize( "{" .. str:gsub( "{", "{;" ) .. "}" )
end

It worked, but i am writing this with this program too, let me show you how am i doing:

				print("Nome:")
  local nome = read()
  print("Idade: ")
  local valor1 = read()
  print("Cidade: ")
  local valor2 = read()
  local table = {
  Nome=nome,
  Idade = valor1,
  Cidade = valor2
  }

  local table2 = textutils.serialize(table)
  local f = fs.open("/lista", "a")
  f.writeLine(table2)
  f.close()

in this way, i dont know how to put the others { }
Edited on 31 August 2015 - 10:15 PM
HPWebcamAble #4
Posted 01 September 2015 - 03:42 AM


local person1 = { "test", "table" }
local person2 = { "another", "table" }

local all = {}

table.insert( all, person1 ) --# table.insert automatically puts something into a table at the first free numerical index
table.insert( all, person2 ) 

local f = fs.open("file")
f.write( textutils.serialize(all) )
f.close()
NOTE: DON'T name a variable 'table', that will override the entire table API!
MKlegoman357 #5
Posted 01 September 2015 - 07:57 AM
To modify the file, you would first load all the data it already has. Then you would edit the loaded table and save that to the file.