Jump to content




Read and Modifiying a Table in a File


  • You cannot reply to this topic
4 replies to this topic

#1 Hayden_Almeida

  • Members
  • 49 posts

Posted 31 August 2015 - 09: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 by Hayden_Almeida, 31 August 2015 - 09:08 PM.


#2 Exerro

  • Members
  • 801 posts

Posted 31 August 2015 - 10: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 by awsumben13, 31 August 2015 - 10:23 PM.


#3 Hayden_Almeida

  • Members
  • 49 posts

Posted 31 August 2015 - 11:15 PM

View Postawsumben13, on 31 August 2015 - 10:22 PM, said:

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 by Hayden_Almeida, 31 August 2015 - 11:15 PM.


#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 01 September 2015 - 02: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!

#5 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 01 September 2015 - 06: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users