Jump to content




Create tables in Lua


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

#1 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

Posted 08 November 2016 - 10:34 PM

How would i create a table in lua?

I know how to make one like a = {} but what about in Lua code? something like table.new(tablename)

Thanks!

- House

#2 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 08 November 2016 - 10:41 PM

{} creates a table this can be assigned to a variable name, like your a example, or anonymous, perhaps passed to a function.

There is not any other way that I'm aware of.

#3 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

Posted 08 November 2016 - 10:46 PM

I actually just found out that i can do

a["x"] = "Test"

then i typed a and it said
{
a = "Test",
}

EDIT: But how would i read it then?
- House

Edited by houseofkraft, 08 November 2016 - 10:47 PM.


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 09 November 2016 - 01:22 AM

You had previously declared a to be a table, then. You can't simply start placing things into any old variable without declaring it to be a table first. What are you actually trying to do? Including more details will help us clear up whatever might be confusing you, and enable us to better answer your actual question.

#5 Anavrins

  • Members
  • 775 posts

Posted 09 November 2016 - 02:27 AM

This kind of information is easily found in the CC wiki, and on other Lua guides on google.
http://www.computerc...nfo/wiki/Tables

#6 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 09 November 2016 - 01:37 PM

As Anavirns says, you can google it, these are some basics in Lua, but wanna introduce you:
local tPerson = {} -- Constructs a table
local tPreparedTable = { 1, 4, x = 15, alpha = "80*" }
-- [[
tPreparedTable contains the indexes 1 to 2.
print(tPreparedTable[1]) -- 1
print(tPreparedTable[2]) -- 4
It contains also the indexes x (value: 15) abd alpha (value (string) 80)
]]
tPerson.Anavrins= {} -- Construct in tPersons a table for Anavirns
tPerson.Anavrins.name = "Anavrins"
print(tPerson.Anavirns.name) --> Anavirns
tPerson.Anavrins.name = "is not Anavrins"
for sName, tData in pairs(tPersons) do -- Iterate through every index in tPersons and set sName to the key and tData to the value
  print(sName, tData.Name) --> Anvairns is not Anavirns
end
It just give you a little, very little view of the power of tables.

#7 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

Posted 09 November 2016 - 08:16 PM

View PostLyqyd, on 09 November 2016 - 01:22 AM, said:

You had previously declared a to be a table, then. You can't simply start placing things into any old variable without declaring it to be a table first. What are you actually trying to do? Including more details will help us clear up whatever might be confusing you, and enable us to better answer your actual question.

I know how to make tables, you can just do a = {} but how do i make the code make the table without having to manually go into the code and add it. I want to make a virtual filesystem and i want it to make a table called the file name and when the user writes. It will add that line the user wrote to the table.

#8 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 09 November 2016 - 08:27 PM

I didn't got it. Do you want a function returning a table? Or do you want to save it onto a file?
But if you want virtual filesystems, you can use FSector (API), i coded it exactly for that =)

Edited by Sewbacca, 09 November 2016 - 08:27 PM.


#9 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 09 November 2016 - 09:10 PM

View Posthouseofkraft, on 09 November 2016 - 08:16 PM, said:

View PostLyqyd, on 09 November 2016 - 01:22 AM, said:

You had previously declared a to be a table, then. You can't simply start placing things into any old variable without declaring it to be a table first. What are you actually trying to do? Including more details will help us clear up whatever might be confusing you, and enable us to better answer your actual question.

I know how to make tables, you can just do a = {} but how do i make the code make the table without having to manually go into the code and add it. I want to make a virtual filesystem and i want it to make a table called the file name and when the user writes. It will add that line the user wrote to the table.

Variable names should describe data, not be data. Having the name of the file which the user is editing is data and therefore should not be the variable name.

If you really want this then you could use nested tables.
--# your code
openFiles = {}
fileName = "example"
openFiles[fileName] = {}
--# user code
openFiles.example[1] = "this is line 1"
openFiles.example[2] = "this is line 2"

This just looks difficult to use in my opinion.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users