赞
踩
一、table
table是lua的一种数据结构来帮助我们创建不同的数据结构,如:字典、数组
eg:
mytable = {a = 'python',b = 'java',c = 'php'}
print(mytable['a'])
1、table中key只能是字符串,这里的a、b、c都是字符串,但是都不能加上引号;如果通过key来访问table的值,这时候key必须带上引号
错误写法:
mytable = {1 = 'oracle',2 = 'mysql',3 = 'leveldb'}
mytable = {'a' = 'hbase','b' = 'redis','c' = 'pg'}
2、table中可以出现没有key的项,这个时候,lua会把table当作数组来对待
eg:
mytable = {a = 'dictor',b = 'actor','banker'}
print(mytable[1])
当table中某项没有指定key时,key就会从1开始自增,上述例子中mytable[1] = ‘banker’
二、数组
数组就是相同数据类型的元素按照一定顺序的集合,数组中的key都是自增的数字
eg:
mytable = {'banana','apple','pine'}
综上所述:table是字典和数组的混合体,当没有key时,table为数组,当指定具体的key时,table为字典
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。