Lua:在表中搜索键,如果不存在则增加索引并重试。

情况:

table = { this,that,something else,x_coord,y_coord }
table.x_coord = { 1,2,3,4,7,8, n }
table.y_coord = { 2,4,5,9,n }-数字不固定
table.check_boxes = {[12]=一个函数,
                      [14]=一个函数,
                      [15]=一个函数,
                      [24]=一个函数,
                      [29]=一个函数,
                         ....(n)}

正如您所看到的,x / y_coord形成check_boxes。例如:

table.x_coord[1]..table.y_coord[1]〜table.check_boxes[1]

我用这个在终端内移动光标到复选框之间。

问题现在是在我的游标动作中。 目前,我有一个函数,它根据给定的输入(箭头键)搜索下一个左/右/上/下的x / y_coord。 用return / space我调用复选框后面的函数。

现在,这可能会将光标设置在未给出复选框的位置。实际上,这并不是一个大问题,因为当input == space / return时,inputhandler调用函数

table.check_boxes[table.x_coorx[x_index]..table.y_coords[y_index]]

所以如果光标没有指向函数,就什么都不会发生。 但是现在我想强制将光标移到下一个复选框。我能做什么?

到目前为止我的想法:

根据左/右/上/下的输入以下功能:

while true do
    for k,v in pairs(table.check_boxes) do
        if(table.x_coord[x_index] .. table.y_coord[y_index] == k then break end
    end -- break->好的,坐标在复选框上

    x_index = x_index +1--或-1

    if table.x_coord[x_index]== nil then
        x_index = 1
    结束
end

现在的问题是最后一个if不会允许类似于x_coord = {1,3}的情况,因为如果到达2,它将将x_index设置为1。

有什么技巧吗?

编辑: 现在我在继续前进:

function cursorTioNextBoxRight()
    searc_index = x_index
    search = true
    while search do
        search_index = search_index + 1

        if search_index>#table.x_coord then
            search_index = 1
        end

    for k,v in pairs(table.check_boxes) do
        if tonumber(table.x_coord[search_index..table.y_coord[y_index]== k then
            x_index = search_index--YAAAY
            search = false
            break
         end
     结束
end

它太慢了。

点赞
用户2458544
用户2458544
本地变量 x_newIndex 等于 x_index + 1 --[[ 或者 -1 --]]
x_index = table.x_coord[x_newIndex] and x_newIndex or x_index

当 x_newIndex 在表中存在时,x_index 的值会变为 x_newIndex,否则会保持原来的 x_index 值。

2014-08-12 07:31:10
用户3911769
用户3911769
function cursorTioNextBoxRight()
search_index = x_index
search = true
while search do
    search_index = search_index + 1

    if search_index > #table.x_coord then
        search_index = 1
    end

    for k, v in pairs(table.check_boxes) do
        if tonumber(table.x_coord[search_index]..table.y_coord[y_index]) == k then
            x_index = search_index -- YAAAY
            search = false
            break
        end
    end
end
2014-08-20 10:22:41