Lua的table的一点点操作---增,删,改,插入,连接,pairs遍历table,ipairs遍历table,使用sort对table进行排序

今天周四,无理由被迫加班中,实在无聊,写个玩的,给我曾经的菜菜生涯一个纪念

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()
    {

    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值