zoukankan      html  css  js  c++  java
  • Lua学习(1)——table

    table类型实现了“关联数组”。“关联数组”是一种具有特殊索引方式的数组。不仅可以通过证书来索引它,还可以使用字符串或其他类型(除了nil)来索引它。table是Lua中主要的数据结构机制(事实也是仅有的),具有强大的功能。基于table可以以一种简单、统一和高效的方式来表示普通数组、符号表、集合、记录、队列和其他数据结构。

    table的特性:

    • table是一个“关联数组”,数组的索引可以是数字或者是字符串
    • table 的默认初始索引一般以 1 开始
    • table 的变量只是一个地址引用,对 table 的操作不会产生数据影响
    • table 不会固定长度大小,有新数据插入时长度会自动增长

     先看table的简单的例子:

    > a={}  //空table
    > k="x"
    > a[k]=10
    > print(a[k])
    10
    > a["x"]=a["x"]+1  //自增
    > print(a["x"])
    11
    

     Lua提供了更简单的”语法糖“(syntactic sugar)

    a.x=10 --print(a["x"]=10)
    print(a.x)  --等于print(a["x"])
    

     table循环读入

    for i=1,5 do
    a[i]=io.read()
    end
    

      table循环输出

    for i=1,#a do
    print(a[i])
    end
    

     #a表示table a最后一个索引


    table 的方法函数

    1. concat

    函数 table.concat 主要用来把表里的每个元素通过一个分隔符(separator)连接组合起来,语法如下:

    table.concat(table, sep,  start, end)
    

      sep、start、end 这三个参数都是可选的,并且顺序读入的,如果没指定传入,函数 concat 会采用默认值(分隔符 sep 的默认值是空字符, start 的默认值是 1, end 的默认值是数组部分的总长)去执行。

    > tbl = {"one", "two", "three", "four"}
    > print(table.concat(tbl))
    onetwothreefour
    > print(table.concat(tbl, " "))
    one two three four
    > print(table.concat(tbl, " ", 2))
    two three four
    > print(table.concat(tbl, " ", 2, 3))
    two three
    

    字符连接还有".."方式

    a=a.."two"
    

    2. insert

    函数 table.insert 用于向 table 的指定位置(pos)插入一个新元素,语法:

    table.insert(table, pos,value)
    

     pos插入位置,可选项

    > a={"one","two"}
    > table.insert(a,"three")
    > print(table.concat(a," "))
    one two three
    

    3. remove

     语法:

    table.remove (tb,pos)
    

     参数 pos 可选,默认为删除 table 最后一个元素,并且参数 pos 的类型只能是数字 number 类型。

    > tb={"one","two","three","four"}
    > table.remove(tb,1)
    > print(table.concat(tb," "))
    two three four
    > table.remove(tb)
    > print(table.concat(tb," "))
    two three
    

    4.maxn

    函数 table.maxn 是返回 table 最大的正数索引值,语法:

    table.maxn(tb)
    
    >tb={"one","two","three"}
    > table.maxn(tb)
    > print(table.maxn(tb))
    3
    

    5. sort

    函数 table.sort 用于对 table 里的元素作排序操作,语法

    table.sort(table,comp)
    

     comp是一个比较函数

    >tb={"one","two","three"}
    > sort_comp = function(a, b) return a > b end
    > table.sort(tb,sort_comp)
    > print(table.concat(tb," "))
    two three one
    

    6. unpack

    函数 table.unpack 用于返回 table 里的元素,语法:

    unpack(table, start, end)
    

     参数 start 是开始返回的元素位置,默认是 1,参数 end 是返回最后一个元素的位置,默认是 table 最后一个元素的位置,参数 start、end 都是可选

    > a = {"one","two","three"}
    > print(unpack(a))
    one     two     three
    > print(unpack(a,2))
    two     three
    > print(unpack(a,2,3))
    two     three
    

    7. pack

    函数 table.pack 是获取一个索引从 1 开始的参数表 table,并会对这个 table 预定义一个字段 n,表示该表的长度, 语法:

    table.pack(···)
    

     该函数常用在获取传入函数的参数。

    #!/usr/local/bin/lua
    function table_pack(...)
            args = pack(...)
            print(args.n)
            tmp={"one","two"}
            print(tmp.n)
    end
    table_pack("test","arg2","arg3")
    
  • 相关阅读:
    AtCoder Grand Contest 015 题解
    AtCoder Grand Contest 014 题解
    AtCoder Grand Contest 013 题解
    AtCoder Grand Contest 012 题解
    AtCoder Grand Contest 011 题解
    AtCoder Grand Contest 010 题解
    AtCoder Grand Contest 009 题解
    NOIP2017 Day2 题解
    博客园主题备份
    多项式全家桶
  • 原文地址:https://www.cnblogs.com/oldtrafford/p/3811793.html
Copyright © 2011-2022 走看看