lua学习笔记 4 迭代法遍历 table,当Table中含Table时,递归输出
2015-05-23 15:42
633 查看
迭代法遍历 table,当Table中含Table时,递归调用。打印Table中 K, V值
通过type(arg) 判断当前类型
table1 = {
name = "Android Developer",
email = "hpccns@gmail.com",
url = "http://blog.csdn.net/hpccn",
quote = [[
There are
10 types of pepole
who can understand binary.
]],--多行文字
embeddedTab = {
em1 = xx,
x =0,
{x =1, y =2 } -- 再内嵌table
}-- 内嵌table
}
tab = " "
function print_table(t, i)
local indent ="" -- i缩进,当前调用缩进
for j = 0, i do
indent = indent .. tab
end
for k, v in pairs(t) do
if (type(v) == "table") then -- type(v) 当前类型时否table 如果是,则需要递归,
print(indent .. "< " .. k .. " is a table />")
print_table(v, i + 1) -- 递归调用
print(indent .. "/> end table ".. k .. "/>")
else -- 否则直接输出当前值
print(indent .. "<" .. k .. "=" .. v.."/>")
end
end
end
print_contents(table1, 0)
输出结果:
for k,v in pairs(table) do 这样的遍历顺序并非是table中table的排列顺序,而是根据table中key的hash值排序来遍历的。
与Java中 HashMap, C++中的Map相似。
[plain] view
plaincopy
<name=Android Developer/>
<quote= There ar
10 types of pepole
who can understand binary.
/>
<url=http://blog.csdn.net/hpccn/>
< embeddedTab is a table />
< 1 is a table />
<y=2/>
<x=1/>
/> end table 1/>
<x=0/>
/> end table embeddedTab/>
<email=hpccns@gmail.com/>
学习重点:
1 数据类型的判断: type()
lua语言中的数据类型主要有:nil、number、string、function、table、thread、boolean、userdata。
需要确定一个变量的类型时,可以使用内置函数type获取,如下:
[plain] view
plaincopy
type(“hello world”); ---->string
type(type); ---->function
type(3.1415); ---->number
type(type(type)); ---->string
2 迭代法
pairs 迭代全部的项
ipairs 迭代以数字做键值的项,且从1 开始
[plain] view
plaincopy
for k, v in pairs(t) do
通过type(arg) 判断当前类型
table1 = {
name = "Android Developer",
email = "hpccns@gmail.com",
url = "http://blog.csdn.net/hpccn",
quote = [[
There are
10 types of pepole
who can understand binary.
]],--多行文字
embeddedTab = {
em1 = xx,
x =0,
{x =1, y =2 } -- 再内嵌table
}-- 内嵌table
}
tab = " "
function print_table(t, i)
local indent ="" -- i缩进,当前调用缩进
for j = 0, i do
indent = indent .. tab
end
for k, v in pairs(t) do
if (type(v) == "table") then -- type(v) 当前类型时否table 如果是,则需要递归,
print(indent .. "< " .. k .. " is a table />")
print_table(v, i + 1) -- 递归调用
print(indent .. "/> end table ".. k .. "/>")
else -- 否则直接输出当前值
print(indent .. "<" .. k .. "=" .. v.."/>")
end
end
end
print_contents(table1, 0)
输出结果:
for k,v in pairs(table) do 这样的遍历顺序并非是table中table的排列顺序,而是根据table中key的hash值排序来遍历的。
与Java中 HashMap, C++中的Map相似。
[plain] view
plaincopy
<name=Android Developer/>
<quote= There ar
10 types of pepole
who can understand binary.
/>
<url=http://blog.csdn.net/hpccn/>
< embeddedTab is a table />
< 1 is a table />
<y=2/>
<x=1/>
/> end table 1/>
<x=0/>
/> end table embeddedTab/>
<email=hpccns@gmail.com/>
学习重点:
1 数据类型的判断: type()
lua语言中的数据类型主要有:nil、number、string、function、table、thread、boolean、userdata。
需要确定一个变量的类型时,可以使用内置函数type获取,如下:
[plain] view
plaincopy
type(“hello world”); ---->string
type(type); ---->function
type(3.1415); ---->number
type(type(type)); ---->string
2 迭代法
pairs 迭代全部的项
ipairs 迭代以数字做键值的项,且从1 开始
[plain] view
plaincopy
for k, v in pairs(t) do
相关文章推荐
- lua学习笔记 4 迭代法遍历 table,当Table中含Table时,递归输出
- Lua 学习笔记:C API 遍历 Table
- Lua 学习笔记之C API 遍历 Table实现代码
- cocos-quick-lua学习笔记---遍历table(in pairs)
- lua学习笔记之table的遍历
- Python学习笔记之遍历目录文件(递归和walk())
- 【Lua学习笔记】 --> 《字符串string与table的函数操作》
- 数据结构学习笔记-二叉树的前、中、后序遍历,递归、非递归方式
- Lua学习笔记 第十七章 弱引用table
- lua学习笔记_table
- Lua 小白关于Table遍历的一点笔记
- lua学习笔记—table
- Lua学习笔记--table
- Lua学习笔记7-table的长度获取
- lua学习笔记15:table数组逆序
- Lua学习笔记-table转为字符串
- lua学习笔记15:table数组逆序
- lua学习笔记_table
- Lua 函数 类 Table --学习笔记
- Lua 学习笔记 (6)table.sort