当前位置:网站首页>The difference between pairs and ipairs
The difference between pairs and ipairs
2022-07-29 06:50:00 【Flytiger1220】
stay lua in ipairs and pairs Can be used to traverse the set , But there's a difference between the two ;
Same as : Can traverse the set ( surface 、 Array ), Both of them are output in order without key Value ;
different : For having key Set :
ipairs From the first number key Start , Output all... In turn key+1 Key value of , Encountering an alphabetic subscript does not end the traversal , Just don't output , If you encounter nil The exit ;
pairs Unordered output letter type key Or number type key Key value of , encounter nil No output , But it won't stop traversing ;
Example 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
Output
-----------ipairs----------------
1 test3
2 val1
-----------pairs----------------
4 val4
1 test3
2 val1
5 val2
Conclusion :
1、ipairs According to key Sequential output data of , Stop outputting when encountering discontinuous data ;
2、pairs All data will be output out of order ;
Example 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
Output
-----------ipairs----------------
1 val3
2 val4
3 test3
4 val2
-----------pairs----------------
1 val3
2 val4
4 val2
test val1
3 test3
Conclusion :
1、pairs and ipairs All priority output without key Of value;
2、pairs All data will be output , No key The values of are output in sequence , belt key The value of is output out of order ;
3、ipairs Will skip the string key, Output digital type in sequence key Value ;
Example 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
Output
-----------ipairs----------------
1 val3
-----------pairs----------------
1 val3
3 val4
11 val2
6 test3
test val1
Conclusion :
1、ipairs encounter nil Will stop output ;
2、pairs encounter nil Output will not stop ;
边栏推荐
- 吴恩达老师机器学习课程笔记 04 多元线性回归
- Hongke solution | a unique solution to realize seamless integration at low cost in Digital Substations
- CNAME记录和A记录的区别
- Right value reference and mobile construction
- 10种常见的软件架构模式
- 【讲座笔记】如何在稀烂的数据中做深度学习?
- Complex floating point multiplication of vivado IP core floating point
- Recurrent neural network RNN
- 20个hacker神器
- Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers
猜你喜欢

5G服务化接口和参考点

Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?

【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配

centos 部署postgresql 13

矩阵分解与梯度下降

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

CNN convolutional neural network

Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?

AbstractQueuedSynchronizer(AQS) 之共享锁源码浅读

CNN-卷积神经网络
随机推荐
基于Matlab解决线性规划问题
Relationship between subnet number, host number and subnet mask
Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
CNN convolutional neural network
Right value reference and mobile construction
PhantomReference 虚引用代码演示
SQL developer graphical window to create database (tablespace and user)
TCP based online dictionary
6、 Network interconnection and Internet
Floating point multiplication and division of vivado IP core floating point
【备忘】关于ssh为什么会失败的原因总结?下次记得来找。
Floating point square root of vivado IP core floating point
Enterprise manager cannot connect to the database instance in Oracle10g solution
Hongke share | bring you a comprehensive understanding of "can bus error" (II) -- can error types
greenplum企业部署
Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?
JMM 内存模型概念
IPv6 representation and configuration cases
SDN拓扑发现原理
MQTT服务器搭建以及使用MQTT.fx测试