当前位置:网站首页>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不会停止输出;
边栏推荐
- Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (2) -- modk operation environment construction
- day02_ Basic grammar
- 什么是DNS放大攻击
- NoClassDefFoundError processing
- 8、 Network security
- Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
- Merkletree builds QT implementation UI
- Raw advanced socket experiment
- 网站受DDoS攻击的表现以及查看方法
- 解决文件大导致磁盘满的问题
猜你喜欢
day06_ Classes and objects
如何在开发板上使用sftp命令访问sftp-server
day15_泛型
Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
What are the advantages of software testing? See how much you know
day13_ Under multithreading
Complex floating point multiplication of vivado IP core floating point
Joint use skills of joiner.on and stream().Map
Thinking about MySQL taking shell through OS shell
day17_集合下
随机推荐
MerkleTree 构建QT实现UI
软件包设置成——>YUM源
Vivado IP核之浮点数加减法 Floating-point
Network Security Learning (I)
day17_ Under collection
day04_数组
Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
Solve the error that the simulation output is STX under the frequency division module Modelsim
day06_类与对象
关于DDoS的几个误区
解决文件大导致磁盘满的问题
Circular linked list and bidirectional linked list
waf防护是什么
Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (2) -- modk operation environment construction
Inventory | major network security events of global key information infrastructure
Day16 set
Floating point multiplication and division of vivado IP core floating point
Arrays&Object&System&Math&Random&包装类
5、 Wireless communication network
非常实用的 Shell 和 shellcheck