大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
值来作数组的索引
,但这个值不能是 nil。同样,在
C语言中,数组的内容只允许一种类型;在 Lua中,你也可以用任意类型的值来作数组的内容,nil也可以。
第一,所有元素之间,总是用逗号 “,” 隔开;
第二,所有索引值都需要用 “[“和”]” 括起来;如果是字符串,还可以去掉引号和中括号; 即如果没有[]括起,则认为是字符串索引
第三,如果不写索引,则索引就会被认为是数字,并按顺序自动从 1往后编;
=
{“hello”
,33
}
= 4
=
{[tt
]
= “table”
,key
= value
,
[“flag”
]
=
nil
, 11
}
(tab
[tt
])
(tab.key
)
(tab
[1
])
变量(包括字符串),都可以[variable]=value的方式
只要a==b那么tab[a]可以访问到tab[b]的值
索引key是常量,不会随变量的改变而变化该value的key
=
{tb12
=
{bool
=
true
}} — simple, it’s a table IN a table :)
(tb11.tb12.bool
) — works fine, since it’s calling the key and value correctly.
(tab11
[“tb12”
].bool
) –same as line 33
(tab11.tb12
[“bool”
]) –same as line 33
(tab11
[“tb12”
][“bool”
]) –same as line 33
=
{john
=“chips”
,jane
=“lemonade”
,jolene
=“egg salad”
}
= “fruit salad” –changed the value to “fruit salad” instead of “egg salad”
= “fagaso food” — adding a new key-value pair to the container lucky.
=
nil — remove john from giving anything or from being a key.
=
{}; b
= a
;
(a
== b
) –> true
,d
=
{},{};
(c
== d
) –>false
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.
=
{“you”
,“me”
, “him”
,“bill”
}
(name
)
, v
in
ipairs
( name
)
do
( k
,v
)
( a
, b
)
if
string.sub
(a
,2
,2
)
<
string.sub
(b
,2
,2
)
then
return
true
else
return
false
end
(name
, cmp
)
, v
in
ipairs
( name
)
do
( k
,v
)
2. table.insert (table, [pos,] value)
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
.=
{“a”
,“c”
, “d”
}
=
{}
(
table
)
for i
=1
,#
table
do
(i
,
table
[i
])
end
(“before insert:”
)
(foo
)
(foo
,2
,“b”
)
(“after insert”
)
(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.
=
{1
,2
, 3
,4
,5
,6
}
(
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
.
(
table.remove
(abc
,2
))
(“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
)) — 5
=
{[-2
]=3
,[- 1
]=0
}
(table.maxn
(duck
)) — 0
–note: the more closures made, the slower the program would run.
local
function get
()
return n
;
end
local
function inc
(m
)
= n
+m
;
end
return
{get
= get
, inc
= inc
}
= mg1
(50
)
(object.get
())
(object
[“get”
]())
(2
)
(object.get
())
local
function get
(o
)
return o.one
end
local
function inc
(self
, two
)
= self.one
+ two
end
function mg3
(one
)
return
{one
= one
, get
= get
, inc
= inc
}
end
= mg3
(50
)
:get
()
(a
,2
)
(a
:get
())
local T
=
{};
function T
:get
()
return self.n
;
end
function T
:inc
(m
)
= self.n
+ m
;
end
function mg4
( n
)
return
{n
= n
, get
=T.get
, inc
=T.inc
}
end
= mg4
(30
)
(c
:get
())
:inc
(4
)
(c
:get
())
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/120420.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...