lua学习之table类型

关系表类型,这是一个很强大的类型。我们可以把这个类型看作是一个数组。只是 C语言的数组,只能用正整数来作索引; 在Lua中,你可以用任意类型的值来作数组的索引,但这个值不能是&#1

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

关系表类型,这是一个很强大的类型。我们可以把这个类型看作是一个数组。只是 C语言的数组,只能用正整数来作索引; 在Lua中,你可以用任意类型的
来作数组的索引
,但这个值不能是 nil。同样,在
C语言中,数组的内容只允许一种类型;在 Lua中,你也可以用任意类型的值来作数组的内容,nil也可以。
基本介绍

注意三点: 

    第一,所有元素之间,总是用逗号 “,” 隔开;

    第二,所有索引值都需要用 “[“和”]” 括起来;如果是字符串,还可以去掉引号和中括号; 即如果没有[]括起,则认为是字符串索引

    第三,如果不写索引,则索引就会被认为是数字,并按顺序自动从 1往后编;
 
例如:
tt 
= 
{“hello” 
,33
}
value 
= 4
tab 
= 
{[tt
] 
= “table”
,key 
= value
, 
[“flag” 
] 
= 
nil
, 11
}
 
print
(tab
[tt
])
print
(tab.key
)
print
(tab
[
])

 
以上写法都是对的。http://hovertree.com/hovertreescj/
 
look = {[www] = “ok”}这样是不对的,www没有赋值,所以默认为nil因此出错table index is nil
 
temp = 1
tab = {[temp] = 1, 11}
print(tab[temp]) –此时的结果是11,因为11没有显式对应的key,因此从1开始,如果前面定义了,则覆盖其value
 
temp = 2
tab = {[temp] = 1, 11}
temp = 1
print(tab[temp]) — 结果是11,虽然定义时[temp] = 1,但是后来我们改变了temp的值,所以指向另外的key了 

 
 
以上可知:
1.对于字符串,在{}定义时,可以key = value, 也可以[“flag”] = nil,索引都是string类型,对于非nil类型
变量(包括字符串),都可以[variable]=value的方式
2.使用table时,对于字符串,可以通过.的方式访问,也可以通过[]方式访问。tab[a],tab[b],
只要a==b那么tab[a]可以访问到tab[b]的值
3.不管定义索引时用的是常量还是变量,最终table中value的
索引key是常量,不会随变量的改变而变化该value的key
 
嵌套
tb11
= 
{tb12 
= 
{bool 
= 
true
}} — simple, it’s a table IN a table :)
— Call magic!
print
(tb11.tb12.bool 
) — works fine, since it’s calling the key and value correctly.
print
(tab11
[“tb12” 
].bool 
) –same as line 33
print
(tab11.tb12 
[“bool”
]) –same as line 33
print
(tab11
[“tb12” 
][“bool”
]) –same as line 33

 
修改table的value
–Altering a table’s content. Basically manipulating the values of the keys.
lucky
= 
{john
=“chips” 
,jane 
=“lemonade”
,jolene
=“egg salad” 
}
 
lucky.jolene 
= “fruit salad” –changed the value to “fruit salad” instead of “egg salad”
lucky.jerry 
= “fagaso food” — adding a new key-value pair to the container lucky.
lucky.john 
= 
nil — remove john from giving anything or from being a key.
 
table的易变性
 

= 
{}; b 
= a
;
print
(
== b
)  –> true
 
c
,
= 
{},{};
 
print
(
== d
) –>false

 
 

table库函数使用
———————————————————–

1. table.sort (table [, comp])

Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table. If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.

The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

name 
= 
{“you” 
,“me”
, “him”
,“bill” 
}
–table.sort – only works with arrays!
table.sort
(name
)
for k
, v 
in 
ipairs
( name
) 
do
     
print
( k
,v
)
end
–table.sort uses callbacks. a function that is writtent to be called by a library function.
function cmp
( a
, b
)
     
if 
string.sub
(a
,
,2
) 
< 
string.sub
(b
,
,2
) 
then
          
return 
true
     
else
          
return 
false
     
end
end
 
table.sort
(name
, cmp
)
for k
, v 
in 
ipairs
( name
) 
do
     
print
( k
,v
)
end

 
 

2. table.insert (table, [pos,] value)

 
Inserts element value at position pos in table, shifting up other elements to open space, if necessary. The default value for pos is n+1, where n is the length of the table so that a call table.insert(t,x) inserts x at the end of table t.
 
–table.insert –an easy to copy a table to another table or adding elements to an array.!
foo 
= 
{“a” 
,“c”
, “d”
}
bar 
= 
{}
function printt
( 
table
)
    
for i
=
,#
table 
do
         
print
(i
,
table 
[
])
    
end
end
print
(“before insert:” 
)
printt
(foo
)
table.insert
(foo
,
,“b”
)
print
(“after insert” 
)
printt
(foo
)

 
 
3.  table.concat (table [, sep [, i [, j]]])
 

Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. If i is greater than j, returns the empty string. 

 
 
–table.concat does what it implies. Takes an array and concates to one string.
num 
= 
{
,2
, 3
,4
,
,6
}
print
(
table.concat 
(num 
,“<”
))
 
 
 

4. table.remove (table [, pos])

Removes from table the element at position pos, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value for pos is n, where n is the length of the table, so that a call table.remove(t) removes the last element of table t.

 
abc = {“a” ,“b”, “c”}
print
(
table.remove 
(abc 
,2
))
print
(“abc length = ” 
.. 
#abc
)
 
 

5. table.maxn (table)

Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)

–table.maxn
apple = {“a” ,“p”,[ 5]=“e”}
print
(table.maxn 
(apple 
)) — 5
 
duck 
= 
{[-
]=3
,[- 1
]=0
}
print
(table.maxn 
(duck 
)) — 0
 
 
面向对象编程
 
–note for a object to work, it needs a closure(inner function with an upvalue(a local value from a higher scope))
–note: the more closures made, the slower the program would run.
function mg1( n)
    
local 
function get 
()
         
return n 
;
    
end
    
local 
function inc 
(
)
        n 
= n 
+
;
    
end
    
return 
{get 
= get
, inc
= inc
}
end
 
object 
= mg1
(50 
)
print
(object.get 
())
print
(object
[“get” 
]())
 
object.inc
(
)
print
(object.get 
())
—————————————-
do
    
local 
function get 
(
)
         
return o.one
    
end
    
local 
function inc 
(self 
, two 
)
        self.one 
= self.one 
+ two
    
end
    
function mg3 
(one 
)
         
return 
{one 
= one 
, get 
= get 
, inc 
= inc 
}
    
end
end

= mg3
(50 
)
a
:get
()
a.inc
(a
,
)
print
(a
:get
())
 
—————————————-
do
    
local T 
= 
{};
    
function T
:get
()
         
return self.n 
;
    
end
    
function T
:inc
(m
)
        self.n 
= self.n 
+ m 
;
    
end
    
function mg4 
( n 
)
         
return 
{
= n 
, get 
=T.get 
, inc 
=T.inc 
}
    
end
end
 

= mg4
(30 
)
print
(c
:get
())
c
:inc
(
)
print
(c
:get
())
 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120420.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • TDA2030的功率放大电路详细教程「建议收藏」

    TDA2030的功率放大电路详细教程「建议收藏」本电路可以将是利用运放TDA2030A制作的功率放大器。电源电压为±12V至±22V。输出的最大功率为18W。该电路为深度负反馈电路,输出电压的放大倍数约为Av=R1/R2=32.3(具体放大倍数请参考模电书籍负反馈部分)。其中R4选用大功率水泥电阻,因为空载时流过R4的电流会过大。D1与D2为二极管,有黑线或者银色线的一端为负极。没有标有正负号的电容为无极电容,不需要区别正负极。标有正负…

    2022年5月30日
    49
  • java heap space 什么意思_Java heap space 解决方法(转)

    java heap space 什么意思_Java heap space 解决方法(转)因为程序要从数据读取近10W行记录处理,当读到9W的时候就出现java.lang.OutOfMemoryError:Javaheapspace这样的错误。在网上一查可能是JAVA的堆栈设置太小的原因。跟据网上的答案大致有这两种解决方法:1、设置环境变量setJAVA_OPTS=-Xms32m-Xmx512m可以根据自己机器的内存进行更改,但本人测试这种方法并没有解决问题。可能是还有…

    2022年7月14日
    19
  • Java线程池详解「建议收藏」

    Java线程池详解「建议收藏」文章目录简介什么是线程池银行营业厅案例执行流程创建方式所有创建方式通过ThreadPoolExecutor创建简介什么是线程池线程池(ThreadPool)是一种基于池化思想管理和使用线程的机制。它是将多个线程预先存储在一个“池子”内,当有任务出现时可以避免重新创建和销毁线程所带来性能开销,只需要从“池子”内取出相应的线程执行对应的任务即可。常见的运用池化思想的有:内存池、数据库连接池。使用线程的优点如下:提高线程的利用率提高程序的相应速度便于统一管理线程对象银行营业厅案例假设银行正常可

    2022年7月9日
    20
  • 【停更】Deepfacelab 新手教程

    【停更】Deepfacelab 新手教程欢迎进入本教程,本教程不定期更新本文教程内容更新时间为:2019/2,本文最后更新时间为:2019/3/8欢迎进群讨论,我不是群主,群主的各QQ群号码请看:https://deepfakes.com.cn/index.php/资助升级群这几天朱茵换脸杨幂的事件上了热门,我们群不存在该违法问题。然而因为网上的这个事件,2群排队已经可以说排到了明年。群主表示等风波结束了再考虑新群,毕竟群主不想…

    2022年5月4日
    112
  • mysqltext长度需要设置嘛_text字段类型需要设置大小吗

    mysqltext长度需要设置嘛_text字段类型需要设置大小吗受到@Ankan-Zerob的挑战,这是我对可以存储在以字为单位的每种文本类型中的最大长度的估计:Type|Bytes|Englishwords|Multi-bytewords———–+—————+—————+—————–TINYTEXT|255|…

    2022年8月13日
    9
  • jrtplib linux编译,linux下jrtplib-3.9.1编译与安装.txt[通俗易懂]

    一、安装包准备1、进入官网http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib下载jrtplib-3.9.1.tar.gz包,路径为:http://research.edm.uhasselt.be/jori/jrtplib/jrtplib-3.9.1.tar.gz注意:Download下边有一段话:Thelibrar…

    2022年4月10日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号