Search code examples
lua

Function stuck looping


My code that I am using gets stuck in a loop. I can't fix it and need some help. I am using Lua 5.1

function FigureOutCodeTable(self)
  tabletoreturn = {}
  local letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '}
   local lettercode = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x0A, 0x0B}
  local loopingv2 = 1
  local loopingv3 = 1
  while loopingv2 <= #self do
    while loopingv3 <= #letters do
      if letters[loopingv3] == self[loopingv2] then
        table.insert(tabletoreturn, lettercode[loopingv3])
      loopingv3 = loopingv3 + 1
      end
    end
    if loopingv3 > #self then
      loopingv3 = 0
    end
    loopingv2 = loopingv2 + 1
  end
  return tabletoreturn
end

FigureOutCodeTable(msgt) --a table

Solution

  • Change

      if letters[loopingv3] == self[loopingv2] then
        table.insert(tabletoreturn, lettercode[loopingv3])
      loopingv3 = loopingv3 + 1
      end
    

    To

      if letters[loopingv3] == self[loopingv2] then
        table.insert(tabletoreturn, lettercode[loopingv3])
      end
      loopingv3 = loopingv3 + 1