Add, delete and modify data in LuaTable, LuaArrayTable and LuaDictTable
introduce
Adding, deleting, modifying, and querying Lua tables is a common table operation in Lua and C#. Here we mainly compare how to add, delete, modify, and query tables in Lua and C# respectively.
Lua table
lua table initialization
- mytable = {}
lua remove reference
- mytable = nil
Add data to table in lua
-
Insert an xx data into the index position in xxtable table.insert(xxtab,index,xx)
-
Insert an xx data into the end position of xxtable table.insert(xxtab,xx)
-
Add a yytab table data to the end of xxtabtable.insert(xxtab,yytab)
-
Modify the index position value in xxtab to xxx (no value is equivalent to adding data)xxtab[index] = xxx
Remove data from table in Lua
- Remove specified index data in the table in lua table.remove(xxtab,index)
- Remove the last data in the table in lua table.remove(xxtab)
Connect data in lua table
- lua connection datatable.concat(xxtab) For example {"a","b","c",5,"d "} Output string type "abc5d"
- Use x symbol to connect data in the tabletable.concat(xxtab,“x”) For example {“a”,“b”, "c",5,"d"} Output string type "axbxcx5xd"
- Use x symbol to connect the data from 2 to 4 in the tabletable.concat(xxtab,“x”,2,4) For example { "a","b","c",5,"d"} Output string type "bxcx5"
Sorting data in Lua table
- Lua table default sorting table.sort(table)
- lua table custom function(a,b) conditional sorting table.sort (table, funtion(a,b))
Get the length of lua table
- Get the length of the table in lua #table
Get the maximum value in the table
- Get the maximum value in the xxtab table in luatable.maxn(xxtab) For example, {1,2,3,4,7} Output 7
UnityC#中LuaTable
- LuaTable GetMetaTable()
- 转成LuaArrayTableLuaArrayTable ToArrayTable()
- 转成LuaDictTable LuaDictTable ToDictTable()
- 转成LuaDictTable<K, V> LuaDictTable<K, V> ToDictTable<K, V>()
- 转成object[] object[] ToArray()
- Print table memory address string ToString()
- Modify value and get value
Modify: tab[index] = xxx or tab[“xxx”] = xxx
Modify: table.SetTable< ; T >(string,T)
Modify: table.RawSet<K, V>(K,V)
Modify: RawSetIndex< T >( int, T)
Get: T RawGetIndex< T >(int)
Get: tab[index] or tab["xxx"]
Get: T table.GetTable< T >(string)
Get: V table.RawGet<K, V>(K)
The lua table is as follows
Modify and copy the code as follows:
Print as follows:
- Get length tab.Length
- Func method in LuaTable
Get method GetLuaFunction("key")
Get method RawGetLuaFunction("key")
The execution method has no return value and no parameters func1.Call()
The execution method has no return value func1.Call< T >(T) supports multiple parameters and will not be written in detail here Print as follows: The code is as follows: The lua table is as follows: The execution method has a return value and has parameters R func1.Invoke<T , R>(T ) Support for multiple parameters will not be written in detail here
The execution method has a return value and no parameters R func1.Invoke< R >()
UnityC#中LuaArrayTable、LuaDictTable、LuaDictTable<K,V>
They all inherit from IEnumerable< T > Equivalent to the usage of Array Dictionary List
The following method can be used for traversal
var itor = tab.GetEnumerator();
while (itor.MoveNext())
{
Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
}
itor.Dispose();
It can also assist LuaTable to convert into corresponding List and Dictionary<K,V>