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

How to add more than once separate table in one file

Started by DannySMc, 07 October 2014 - 05:09 PM
DannySMc #1
Posted 07 October 2014 - 07:09 PM
How do you make a file with more than once table in it, that you can access both of them. I need to make a file with many tables of users. Example:

["username1"] = {
	 ["password"] = 1234567890,
	 ["balance"] = 1000000,
	 ["nickname"] = "UserPerson",
	 ["MoreInfo"] = "Something interesting",
	 ["rank"] = "Failure",
}
["username2"] = {
	 ["password"] = 1234567890,
	 ["balance"] = 1000000,
	 ["nickname"] = "UserPerson",
	 ["MoreInfo"] = "Something interesting",
	 ["rank"] = "Failure",
}
["username3"] = {
	 ["password"] = 1234567890,
	 ["balance"] = 1000000,
	 ["nickname"] = "UserPerson",
	 ["MoreInfo"] = "Something interesting",
	 ["rank"] = "Failure",
}
["username4"] = {
	 ["password"] = 1234567890,
	 ["balance"] = 1000000,
	 ["nickname"] = "UserPerson",
	 ["MoreInfo"] = "Something interesting",
	 ["rank"] = "Failure",
}

Then be able to access them with things like:

table.insert("username1", {["MoreStuff"] = "sfdgfgdhth",})
and to remove stuff. But how do I actually change information already there and how do I remove a record?

Thanks
Dragon53535 #2
Posted 07 October 2014 - 07:13 PM
Instead of using the table functions, you could easily just do this.

username1["morestuff"] = "dfaoidufoiu235iu13857a"
--#And then to remove just set it to nil
username1["morestuff"] = nil
KingofGamesYami #3
Posted 07 October 2014 - 07:41 PM
Why do you need multiple tables in your file? Couldn't you just put all the tables inside a table, which you then save to a file?

Also, about saving it… http://www.computercraft.info/forums2/index.php?/topic/20602-autosaving-tables/ shameless self promotion
Edited on 07 October 2014 - 05:41 PM
DannySMc #4
Posted 07 October 2014 - 08:02 PM
Why do you need multiple tables in your file? Couldn't you just put all the tables inside a table, which you then save to a file?

Also, about saving it… http://www.computerc...osaving-tables/ shameless self promotion

The thing is I want to be able to use ipairs and do an iteration of the table so data is placed on a monitor, just I know you can do it with tables, but when you try and do it with tables inside tables it doesn't work :(/>

Instead of using the table functions, you could easily just do this.

username1["morestuff"] = "dfaoidufoiu235iu13857a"
--#And then to remove just set it to nil
username1["morestuff"] = nil

That's a table inside a table?

I also need to iterate it? So I can write information from it to a monitor?
So I can iterate one table and not all of them?

for k, v in ipairs("username1") do
	 print(k..": "..v)
end

and then if I want just load the table and do it with username2's data instead?
KingofGamesYami #5
Posted 07 October 2014 - 08:40 PM
The thing is I want to be able to use ipairs and do an iteration of the table so data is placed on a monitor, just I know you can do it with tables, but when you try and do it with tables inside tables it doesn't work :(/>
What? Iteration works just as well on tables within tables.


local tbl = {
  tbl = { "hello", "world" },
}
for k, v in ipairs( tbl.tbl ) do
  print( v )
end
DannySMc #6
Posted 13 October 2014 - 01:35 PM
The thing is I want to be able to use ipairs and do an iteration of the table so data is placed on a monitor, just I know you can do it with tables, but when you try and do it with tables inside tables it doesn't work :(/>
What? Iteration works just as well on tables within tables.


local tbl = {
  tbl = { "hello", "world" },
}
for k, v in ipairs( tbl.tbl ) do
  print( v )
end

Ahh i see, is there a way of using two seperate files, one with a name and the other with a code? then when I do an iteration it would place them next to each other like so:

the tables:


tName = {
  "Dan",
  "James",
  "John",
  "george",
}
tCode = {
  "WRTWRW",
  "JHKLUY",
  "ABDFGH",
  "HJKLOP",
}

The result:

Dan: WRTWRW
James: JHKLUY
John: ABDFGH
George: HJKLOP

Thanks

The thing is I want to be able to use ipairs and do an iteration of the table so data is placed on a monitor, just I know you can do it with tables, but when you try and do it with tables inside tables it doesn't work :(/>
What? Iteration works just as well on tables within tables.


local tbl = {
  tbl = { "hello", "world" },
}
for k, v in ipairs( tbl.tbl ) do
  print( v )
end

Also I tried to do a:

table.insert(table1, ["Hello"]="lol")
but it doesn't work?

How do I start a table with the usernames at the start? like:

["username1"] = {
  ["lol"] = "jkjdsg",
  ["lel"] = "jkdsksd",
}

so at the start it says username1? how do I make a table like that? with code not manually editing it?
and how do you add say:

["lal"] = "jdksdfgjk"

into say username1's array?
KingofGamesYami #7
Posted 13 October 2014 - 02:04 PM
I don't think you quite understand this.


--#an example table
local allUsers = {
  ["username1"] = {
    ["lol"] = "jkjdag",
    ["lel"] = "jkdsksd",
  },
  ["username2"] = {
    ["lol"] = "fjsdkl",
    ["lel"] = "fjewoi",
  },
}
--#modifying
allUsers[ "username1" ][ "lol" ] = "something"
--#iterating
for k, v in pairs( allUsers[ "username1" ] ) do
  print( k .. " : " .. v )
end

Also, here's how you can load it (instead of manually editing the code).

local allUsers = {}
if fs.exists( "theuserfile" ) then
  local file = fs.open( "theuserfile", "r" )
  allUsers = textutils.unserialize( file.readAll() )
  file.close()
end

And now have a user make their own account

local username
repeat
  term.write( "username: " )
  username = read()
until not allUsers[ username ]
allUsers[ username ] = { ["bla"] = "bla bla", ["etc"]="and so on", }
Edited on 13 October 2014 - 12:05 PM
Dragon53535 #8
Posted 13 October 2014 - 09:17 PM
If you're really wanting iteration you could just set the next numerical index in the table to be the key to the table holding the usernames.


local tbl = { { ["I like piE"] = "noooooo"  },
{ ["I don't like pie"] = "yesssss" } }

print(#tbl) --#This prints 2 since there are 2 things in the first layer of tbl

tbl[#tbl + 1] = { ["Hey look i'm now in the table!"] = "So am i!!!" }

--[[ The table now looks like
{ { ["I like piE"] = "noooooo"  },
  { ["I don't like pie"] = "yesssss" },
  { ["Hey look i'm now in the table!"] = "So am i !!!" } }
]]--
Edited on 13 October 2014 - 07:17 PM