lua学习笔记_table

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

你可能感兴趣的:(table,lua)