Search code examples
lualua-table

finding minimum values from a cut table Lua 5.1.5


I have a Lua script that turns a table into segments:

function tablecut(t, n)
    local result = {}
    local j = 0
    for i = 1, #t do
        if (i-1) % n == 0 then
            j = j + 1
            result[j] = {}
        end
        result[j][#result[j]+1] = t[i]
    end
    return result
end

output = tablecut({'15', '62', '14', '91', '33', '55', '29', '4'}, 4)
for i = 1, #output do
    for j = 1, #output[i] do
        io.write(tostring(output[i][j])..'  ')
    end
    print()
end

output:

15  62  14  91  
33  55  29  4

And I am trying to find the minima from the cut lists so the output would look like this:

15  62  14  91  
min = 14
33  55  29  4
min = 4

Edit: If its of any importance this is how I got it to work on Lua 5.3 but there is no table.move function on Lua 5.1. I can't remember how my thought function worked when I wrote this code.

function indexOf(array, value)
  for i, v in ipairs(array) do
      if v == value then
          return i
      end
  end
  return nil
end


Indicies = {}
Answers = {}

function chunks(lst, size)
  local i = 1
  local count = 0
  return function()
    if i > #lst then return end
    local chunk = table.move(lst, i, i + size -1, 1, {})
    i = i + size
    count = count + 1
    return count, chunk
  end
end

local a = {91,52,19,59,38,29,58,11,717,91,456,49,30,62,43,8,17,15,26,22,13,10,2,23} --Test list
for i, chunk in chunks(a, 4) do
    x=math.min(a)
    print(string.format("#%d: %s", i, table.concat(chunk, ",")))
    table.sort(chunk)
    print(math.min(chunk[1]))
    table.insert(Answers, chunk[1])
    table.insert(Indicies, (indexOf(a, chunk[1])))

Output:

#1: 91,52,19,59
19
#2: 38,29,58,11
11
#3: 717,91,456,49
49

Solution

  • your table cut function could be simplified, and your output for loop needs you use an iterator if you want to get an output simply like you do in your 5.3 script.

    function cuttable(t,n)
      local binned = {}
      
      for i=1,#t,n do
          local bin = {}
            for j=1,n do
                table.insert(bin, t[i + ((j - 1) % n)])
            end
          table.insert(binned, bin)
      end
      
      return binned
    end
    

    For the for loop, we can use ipairs on the output of cuttable keeping things pretty simple, then we just do the same steps of concat then sort and print out our results.

    for k, bin in ipairs(cuttable(a,4)) do
        local output = "#" .. k .. ":" .. table.concat(bin, ",")
        table.sort(bin)
        print(output)
        print(bin[1])
    end
    

    Output

    #1:91,52,19,59
    19
    #2:38,29,58,11
    11
    #3:717,91,456,49
    49
    #4:30,62,43,8
    8
    #5:17,15,26,22
    15
    #6:13,10,2,23
    2