今天周四,无理由被迫加班中,实在无聊,写个玩的,给我曾经的菜菜生涯一个纪念
Lua的table的一点点操作
1.初始化一个table
2.循环对table插入数字,键值为字符串
3 根据键删除table中元素
4 修改table中元素
5 使用pairs遍历table
6 使用ipairs遍历table
7 使用sort对table升序排序
8 使用sort对table进行降序排序
直接上代码
Unity = CS.UnityEngine
txttable = {'a', '33', 'b', 'e', '11', 'f', 'c', 'd'}
txt = '--------\r\n'
yuan = '原表数据:\r\n'
pai = '排序后:\r\n'
yi = '按索引删除后的数据:\r\n'
cha = '按索引插入后的数据:\r\n'
lian = '连接后的数据:\r\n'
--原表数据输出
for k, v in ipairs(txttable) do
yuan = yuan .. ' ' .. v
end
--排序后输出
table.sort(txttable)
for k, v in ipairs(txttable) do
pai = pai .. ' ' .. v
end
--按索引删除后输出
table.remove(txttable, 1)
for k, v in ipairs(txttable) do
yi = yi .. ' ' .. v
end
--按索引插入后输出
table.insert(txttable, 4, '猜猜')
for k, v in ipairs(txttable) do
cha = cha .. ' ' .. v
end
--连接后输出
lian = lian..table.concat(txttable, '???', 1, 2)
--UI界面输出
function WriterText()
text = Unity.GameObject.Find('Text'):GetComponent('Text')
txt = yuan .. '\r\n' .. pai .. '\r\n' .. yi .. '\r\n' .. cha .. '\r\n' .. lian
text.text = txt
end
WriterText()
--有个实习生,我擦擦,电子科大的,然后来问我表操作,我就又写了一个,
Unity = CS.UnityEngine
local tab = {99, 5, 1, 45, 27, 9, 16, 32, 86}
function Writer(t)
for i = 1, #t do
print(t[i])
end
end
table.sort(tab)
print('排序后')
Writer(tab)
print('降序后:::')
table.sort(
tab,
function(a, b)
return (a > b)
end
) -- 降序
Writer(tab)
table.remove(tab, 1)
table.remove(tab, 2)
print('删除后')
Writer(tab)
print('循环插入后::')
for i = 1, #tab do
table.insert(tab, i, 'b')
end
Writer(tab)
tab[1] = 'aaa'
print('修改后')
Writer(tab)
print('pairs::')
for k, v in pairs(tab) do
print(k, v)
end
print('\r\n' .. '*******************')
print('ipairs::')
tab[3] = nil
for k, v in ipairs(tab) do
print(k, v)
end
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XLua;
public class table : MonoBehaviour
{
LuaEnv luaEnv;
Text text;
// Start is called before the first frame update
void Start()
{
luaEnv = new LuaEnv();
luaEnv.DoString("require('xlua/main')");
}
// Update is called once per frame
void Update()
{
}
}