当前位置:网站首页>pairs和ipairs的区别
pairs和ipairs的区别
2022-07-29 05:26:00 【Flytiger1220】
在lua中ipairs 和pairs均可以用来遍历集合,但是两者有区别;
同:都是能遍历集合(表、数组),两者均优先按顺序输出没有key的值;
异:对于有key的集合:
ipairs从第一个数字key开始,依次输出所有的key+1的键值,遇到字母下标并不会结束遍历,只是不输出而已,如果遇到nil则退出;
pairs无序输出字母类型key或者数字类型key的键值,遇到nil不输出,但不会停止遍历;
例子1:
table = {
[1] = "test3", [2] = "val1" , [5] = "val2", [4] = "val4" }
print("-----------ipairs----------------")
for k,v in ipairs(table) do
print(k,v)
end
print("-----------pairs----------------")
for k,v in pairs(table) do
print(k,v)
end
输出
-----------ipairs----------------
1 test3
2 val1
-----------pairs----------------
4 val4
1 test3
2 val1
5 val2
结论:
1、ipairs会按照key的顺序输出数据,遇到不连续的数据停止输出;
2、pairs会无序输出所有数据;
例子2:
table = {
[3] = "test3", ["test"] = "val1", "val3" , [4] = "val2", "val4" }
print("-----------ipairs----------------")
for k,v in ipairs(table) do
print(k,v)
end
print("-----------pairs----------------")
for k,v in pairs(table) do
print(k,v)
end
输出
-----------ipairs----------------
1 val3
2 val4
3 test3
4 val2
-----------pairs----------------
1 val3
2 val4
4 val2
test val1
3 test3
结论:
1、pairs和ipairs均优先输出没有key的value;
2、pairs会输出所有的数据,不带key的值按顺序输出,带key的值无序输出;
3、ipairs会跳过字符串的key,按顺序输出数字型key的值;
例子3
table = {
[6] = "test3", ["test"] = "val1", "val3" , [11] = "val2", nil, "val4" }
print("-----------ipairs----------------")
for k,v in ipairs(table) do
print(k,v)
end
print("-----------pairs----------------")
for k,v in pairs(table) do
print(k,v)
end
输出
-----------ipairs----------------
1 val3
-----------pairs----------------
1 val3
3 val4
11 val2
6 test3
test val1
结论:
1、ipairs遇到nil会停止输出;
2、pairs遇到nil不会停止输出;
边栏推荐
- MQTT服务器搭建以及使用MQTT.fx测试
- Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus
- 3、 Wide area communication network
- Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
- day02_ Basic grammar
- Ultra low cost DDoS attacks are coming. See how WAF protects Jedi
- Ansible(自动化软件)
- 解决文件大导致磁盘满的问题
- 如何画出优秀的架构图
- TCP based online dictionary
猜你喜欢

Hongke solution | a unique solution to realize seamless integration at low cost in Digital Substations

FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters

6、 Network interconnection and Internet

What are the advantages of software testing? See how much you know

2022 summer second day information competition learning achievement sharing 2

Network Security Learning (II)

day13_ Under multithreading

day02_ Basic grammar

day16-集合上

day12_ Multithreading
随机推荐
MerkleTree 构建QT实现UI
8、 Network security
Conversion of fixed-point number to floating-point number of vivado IP core
Idea installing Scala
超低成本DDoS攻击来袭,看WAF如何绝地防护
Why are the job requirements for software testing in 2022 getting higher and higher? Is there any secret in it?
Floating point multiplication and division of vivado IP core floating point
软件测试的优势有哪些?看看你了解多少.....
服务器常见故障及其解决方法
IPv6表示方法与配置案例
2022年的软件测试的岗位要求为何越来越高?这其中有什么不可告人的秘密吗?
7、 Next generation Internet IPv6
Phishing mail disposal
MQTT服务器搭建以及使用MQTT.fx测试
[interview questions] the latest software test interview questions in 2022 (400) [with answers] continue to update
网络工具中的“瑞士军刀”-nc
解决文件大导致磁盘满的问题
greenplum企业部署
用神经网络实现手写数字识别
Plugin location in mavan