求助:在lua用table.insert()向表中插入元素时发生的奇怪现象,有的元素有key,有的没有?
notno 2008-11-29 10:14:45 搞不懂下面这几行代码的输出:
str = "welcome to lua world \13y"
str_table = {}
for line in str:gmatch('([^\n]+)') do
print(line,type(line))
table.insert(str_table, line)
end
print("##############")
for k,v in pairs(str_table) do
print(k,v)
end
print("##############")
print(#str_table)
输出为:
welcome to lua world --为什么这个“字符串”的类型是空的?
y string
##############
1 welcome to lua world
y --为什么这行没有序号?
##############
1
附:
table.insert (table, [pos,] value)
Inserts element 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 (see §2.5.5), so that a call table.insert(t,x) inserts x at the end of table t.