Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Lua循环知道循环何时结束_Loops_Lua - Fatal编程技术网

Loops Lua循环知道循环何时结束

Loops Lua循环知道循环何时结束,loops,lua,Loops,Lua,Lua中是否有一个语句允许我确定它是否是最后一个循环周期? 当我不能确定有多少时间循环时 例如: for _, v in pairs(t) do if I_know_this_is_your_last_cycle then -- do something end 一般来说,不会。正如您从中看到的,for循环是迭代器上的语法sugar for a while循环,因此它只知道循环是否在循环开始时结束 如果您真的想检查您是否正在进入最后一个迭代,那么我只需要使用while循环显式地编写代码 loc

Lua中是否有一个语句允许我确定它是否是最后一个循环周期? 当我不能确定有多少时间循环时

例如:

for _, v in pairs(t) do
if I_know_this_is_your_last_cycle then
-- do something
end

一般来说,不会。正如您从中看到的,for循环是迭代器上的语法sugar for a while循环,因此它只知道循环是否在循环开始时结束

如果您真的想检查您是否正在进入最后一个迭代,那么我只需要使用while循环显式地编写代码

local curr_val = initial_value
while curr_val ~= nil then
    local next_val = get_next(initial_value)
    if next_val ~= nil then
       --last iteration
    else
       --not last iteration
    end
    curr_val = next_val
end
如果要使用pairs函数翻译示例,可以将该函数用作迭代器


顺便说一句,我建议在编写这样的循环之前要三思。它的编码方式意味着很容易编写在迭代0或1个元素时无法正常工作的代码,或者编写无法正确处理最后一个元素的代码。大多数情况下,编写一个普通循环并将末尾代码放在循环之后更为合理。

一般来说,不是。从中可以看出,for循环是迭代器上的while循环的语法sugar,因此它只知道循环是否在循环的开始处结束

如果您真的想检查您是否正在进入最后一个迭代,那么我只需要使用while循环显式地编写代码

local curr_val = initial_value
while curr_val ~= nil then
    local next_val = get_next(initial_value)
    if next_val ~= nil then
       --last iteration
    else
       --not last iteration
    end
    curr_val = next_val
end
如果要使用pairs函数翻译示例,可以将该函数用作迭代器


顺便说一句,我建议在编写这样的循环之前要三思。它的编码方式意味着很容易编写在迭代0或1个元素时无法正常工作的代码,或者编写无法正确处理最后一个元素的代码。大多数情况下,编写一个简单的循环并将代码放在循环后面更合理。

有几种方法可以做到这一点。最简单的方法是使用标准for loop并自己检查,如下所示:

local t = {5, nil, 'asdf', {'a'}, nil, 99}
for i=1, #t do
    if i == #t then
        -- this is the last item.
    end
end
function notifyPairs(t)
    local lastItem = false
    local i = 0
    return
      function()
        i = i + 1
        if i == #t then lastItem = true end;
        if (i <= #t) then
            return lastItem, t[i]
        end
      end
end

for IsLastItem, value in notifyPairs(t) do
    if IsLastItem then 
        -- do something with the last item
    end
end
    --count how many elements you have in the table
    local element_cnt = 0
    for k,v in pairs(t) do
      element_cnt = element_cnt + 1
    end


    local current_item = 1
    for k,v in pairs(t)
       if current_item == element_count then
         print  "this is the last element"
       end
       current_item = current_item + 1
    end
或者,您可以为表滚动自己的迭代器函数,该函数会告诉您何时处理最后一项,类似于以下内容:

local t = {5, nil, 'asdf', {'a'}, nil, 99}
for i=1, #t do
    if i == #t then
        -- this is the last item.
    end
end
function notifyPairs(t)
    local lastItem = false
    local i = 0
    return
      function()
        i = i + 1
        if i == #t then lastItem = true end;
        if (i <= #t) then
            return lastItem, t[i]
        end
      end
end

for IsLastItem, value in notifyPairs(t) do
    if IsLastItem then 
        -- do something with the last item
    end
end
    --count how many elements you have in the table
    local element_cnt = 0
    for k,v in pairs(t) do
      element_cnt = element_cnt + 1
    end


    local current_item = 1
    for k,v in pairs(t)
       if current_item == element_count then
         print  "this is the last element"
       end
       current_item = current_item + 1
    end

有几种方法可以做到这一点。最简单的方法是使用标准for loop并自己检查,如下所示:

local t = {5, nil, 'asdf', {'a'}, nil, 99}
for i=1, #t do
    if i == #t then
        -- this is the last item.
    end
end
function notifyPairs(t)
    local lastItem = false
    local i = 0
    return
      function()
        i = i + 1
        if i == #t then lastItem = true end;
        if (i <= #t) then
            return lastItem, t[i]
        end
      end
end

for IsLastItem, value in notifyPairs(t) do
    if IsLastItem then 
        -- do something with the last item
    end
end
    --count how many elements you have in the table
    local element_cnt = 0
    for k,v in pairs(t) do
      element_cnt = element_cnt + 1
    end


    local current_item = 1
    for k,v in pairs(t)
       if current_item == element_count then
         print  "this is the last element"
       end
       current_item = current_item + 1
    end
或者,您可以为表滚动自己的迭代器函数,该函数会告诉您何时处理最后一项,类似于以下内容:

local t = {5, nil, 'asdf', {'a'}, nil, 99}
for i=1, #t do
    if i == #t then
        -- this is the last item.
    end
end
function notifyPairs(t)
    local lastItem = false
    local i = 0
    return
      function()
        i = i + 1
        if i == #t then lastItem = true end;
        if (i <= #t) then
            return lastItem, t[i]
        end
      end
end

for IsLastItem, value in notifyPairs(t) do
    if IsLastItem then 
        -- do something with the last item
    end
end
    --count how many elements you have in the table
    local element_cnt = 0
    for k,v in pairs(t) do
      element_cnt = element_cnt + 1
    end


    local current_item = 1
    for k,v in pairs(t)
       if current_item == element_count then
         print  "this is the last element"
       end
       current_item = current_item + 1
    end

你可以试着写这样的东西:

local t = {5, nil, 'asdf', {'a'}, nil, 99}
for i=1, #t do
    if i == #t then
        -- this is the last item.
    end
end
function notifyPairs(t)
    local lastItem = false
    local i = 0
    return
      function()
        i = i + 1
        if i == #t then lastItem = true end;
        if (i <= #t) then
            return lastItem, t[i]
        end
      end
end

for IsLastItem, value in notifyPairs(t) do
    if IsLastItem then 
        -- do something with the last item
    end
end
    --count how many elements you have in the table
    local element_cnt = 0
    for k,v in pairs(t) do
      element_cnt = element_cnt + 1
    end


    local current_item = 1
    for k,v in pairs(t)
       if current_item == element_count then
         print  "this is the last element"
       end
       current_item = current_item + 1
    end
或者这个:

local last_key = nil
for k,v in pairs(t) do
   last_key = k
end

for k,v in pairs(t) do
  if k == last_key then
--this is the last element
  end
end

你可以试着写这样的东西:

local t = {5, nil, 'asdf', {'a'}, nil, 99}
for i=1, #t do
    if i == #t then
        -- this is the last item.
    end
end
function notifyPairs(t)
    local lastItem = false
    local i = 0
    return
      function()
        i = i + 1
        if i == #t then lastItem = true end;
        if (i <= #t) then
            return lastItem, t[i]
        end
      end
end

for IsLastItem, value in notifyPairs(t) do
    if IsLastItem then 
        -- do something with the last item
    end
end
    --count how many elements you have in the table
    local element_cnt = 0
    for k,v in pairs(t) do
      element_cnt = element_cnt + 1
    end


    local current_item = 1
    for k,v in pairs(t)
       if current_item == element_count then
         print  "this is the last element"
       end
       current_item = current_item + 1
    end
或者这个:

local last_key = nil
for k,v in pairs(t) do
   last_key = k
end

for k,v in pairs(t) do
  if k == last_key then
--this is the last element
  end
end

这是missingno答案的简化版本:

for _, v in pairs(t) do
  if next(t,_) == nil then
    -- do something in last cycle
  end
end

这是missingno答案的简化版本:

for _, v in pairs(t) do
  if next(t,_) == nil then
    -- do something in last cycle
  end
end

你是说分手声明吗?是的,它存在于Lua中。你能举一个更好的例子说明你为什么需要这样做吗?你所做的对我来说有点代码味道……不,我知道break,但break只会结束循环。对不起,我的例子不好。对于u,v in pairst do if u==last\u cycle\u语句,那么-do something end我在寻找last\u cycle\u语句,如果它存在的话。你是指break语句吗?是的,它存在于Lua中。你能举一个更好的例子说明你为什么需要这样做吗?你所做的对我来说有点代码味道……不,我知道break,但break只会结束循环。对不起,我的例子不好。对于u,v in pairst do if==last_cycle_语句,然后-do something end我在寻找last_cycle_语句,如果它存在的话。第一段代码将只适用于索引为从1开始的连续整数的表。是的,他没有给出他的表的示例,所以我假设他正在构建他的表,就像我在自动生成索引之上一样。第一段代码只适用于从1开始的连续整数索引的表。是的,他没有给出他的表的例子,所以我假设他在构建他的表,就像我在自动生成的索引之上一样。missingno的答案在哪里?@FaheemMitha hugomg==missingno这是他2013年的老绰号谢谢你的澄清。我没有想到改名。missingno的答案在哪里?@FaheemMitha hugomg==missingno这是他2013年的老绰号谢谢你的澄清。我没有想到改名。