lua学习笔记_table

tb = {"junjiex","xjunjie","hello","world" }
    printf("invoke concat result "..table.concat(tb,":"))
    --输出:invoke concat result junjiex:xjunjie:hello:world

   --忽略分割付,输出第二个到第三个
    printf("table.concat(tb,nil,2,3) "..table.concat(tb,nil,2,3))
    --输出:xjunjiehello

   --添加分割符"-",输出第一个到第三个
    printf("table.concat(tb,'-',2,3)"..table.concat(tb,'-',1,3))
    --输出:junjiex-xjunjie-hello

   --默认在末尾插入aaaa
   table.insert(tb,"aaaa")
   printf(table.concat(tb,","))

   --在指定位置插入bbbb
   table.insert(tb,1,"bbbb")
   printf(table.concat(tb,","))

    mytable = {[1] = "a",[2] = "b",[3]="c",[26] = "z" }
    printf("mytable.length = "..#mytable) --#table 是获取其个数,要连续的才能获取,这里输出3,因为26跟前面的3不连续


   --maxn取得最大的key
    printf(table.maxn(mytable))

    tb2 = {k1 = "a",k2 = "b" ,[3] = "c"}
    printf(table.maxn(tb2))

    printf("remove before = "..table.concat(tb,","))
    table.remove(tb)
    printf("remove after = "..table.concat(tb,","))

    table.sort(tb)
    printf("sort = "..table.concat(tb,","))


    guild = {}
    table.insert(guild,
        {
            name = "CladHaire",
            class = "rogue",
            level = 70
        })

    table.insert(guild,
        {
            name = "Sagart",
            class = "Priest",
            level = 70
        })



    len = table.getn(guild)
    printf("table.getn(guild) = "..len)



    --table的嵌套

   key = "x"
    table1 = {table2={[1]= "t2",[2]="t3",key = "y"} }
    printf("table1.table2[1]  "..table1.table2[1])
    printf("table1.table2.key   "..table1.table2.key)


    --修改table中的value
    key = "x"
    tb = {key = "yyy",[1] = 10}
    printf("before tb[key]="..tb.key)
    printf("before tb[1]="..tb[1])

    tb.key = "junjiex"
    tb[1] = 10000

   printf("after tb[key]="..tb.key)
   printf("after tb[1]="..tb[1])

   --给table增加字段
    tb.aaa = "add key"
    printf("tb.aaa =="..tb.aaa)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值