如何按照确切的顺序遍历一个表? [英] How to iterate through a table in its exact order?

查看:148
本文介绍了如何按照确切的顺序遍历一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 本地字母= {DIN1 = hi,AIN1 =my,AIN2 =name,DIN2 =is} 

对于名字,数值成对(字母)做
print(name,value)
end

预期输出:

  DIN1 hi 
AIN1 my
AIN2 name
DIN2 is

输出:

pre $ AIN1 my
DIN2是
DIN1 hi
AIN2名称

如何编写代码以便for循环遍历表订购? (这个命令是怎么定义的)

编辑:我不需要字母顺序,但是和表中定义的顺序一样。



编辑:我需要打印的键和值。在回答Lua成对写入顺序相同的情况下,将只有indexnumber和value被打印出来

解决方案



 函数add(t,k,v,...)
if k〜= nil then
t [k] = v
t [#t + 1] = k
return add(t,...)
end
返回t
结束

t = add({},A,hi,B,my,C,name,D 是)

for i,k in ipairs(t)do
local v = t [k]
print(k,v)
end

当然,这个假设整数键不被任何东西使用,除了 add < code
$ b $ p $ insert $ {code>和$ code删除(t,k)
留给读者练习。

编辑:
省略号(点) add 函数允许根据需要传递尽可能多的参数来一次设置多个kv对。如果没有这个,我们只能设置一个对,比如 add(t,A,hi)。函数定义 add(t,k,v,...)将前三个参数赋值给 t,k,v 并保持别人不变。然后 add 处理第一个对( t [k] = v ),并用其余的 ... 参数。

  tkv ... 
等级1:(t ,A,hi,B,my,C,name,D,is)
level 2:(t,< - B, 我,C,名称,D,是)
等级3:(t,< - C,name,D,is)
4级:(t,< - D,is)
5级:(t,< - )

在等级5处, k v nil s,因为参数列表太短,递归停止。


If i try to output this table, they are looped through in the false order:

local letters   =   {DIN1="hi", AIN1= "my", AIN2 ="name", DIN2="is"}

for name, value in pairs(letters) do
    print(name,value)
end

Expected Output:

DIN1   hi
AIN1   my
AIN2    name
DIN2   is

Output:

AIN1    my
DIN2    is
DIN1    hi
AIN2    name

How can i code it so that the for loop runs through the tables actual order? (The order how it was defined)

Edit: I don't need the alphabetic order, but the same order as in the definition of the table.

Edit: I need to have the key AND the value printed. In the answer "Lua in pairs with same order as it's written" there will be only indexnumber and value printed

解决方案

You may utilize integer part of the table to store keys in order:

function add(t, k, v, ...)
    if k ~= nil then
        t[k] = v
        t[#t+1] = k
        return add(t, ...)
    end
    return t
end

t = add({ }, "A", "hi", "B", "my", "C", "name", "D", "is")

for i,k in ipairs(t) do
    local v = t[k]
    print(k, v)
end

Of course, this assumes that integer keys are not used by anything except add.

insert(t, k, v) and remove(t, k) left as an exercise to the reader.

EDIT: Ellipsis (dots) in add function allow passing as many arguments as needed to set many kv-pairs at once. Without that, we would be only able to set one pair per call, like add(t, "A", "hi"). Function definition add(t, k, v, ...) assigns first three arguments to t, k, v and leaves others untouched. Then add processes first pair (t[k]=v) and recurses with the rest ... of arguments.

          t   k    v    ...
level 1: (t, "A", "hi", "B", "my", "C", "name", "D", "is")
level 2: (t,         <- "B", "my", "C", "name", "D", "is")
level 3: (t,                    <- "C", "name", "D", "is")
level 4: (t,                                 <- "D", "is")
level 5: (t,                                          <- )

At level 5, k and v take nils, because argument list is too short, and recursion stops.

这篇关于如何按照确切的顺序遍历一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆