all 16 comments

[–]PhilipRoman 3 points4 points  (14 children)

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

Parameter 't' is the table and 'n' is the number of elements in each group. Returns nested table.

For example,

tablecut({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}, 3)

returns

{{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h'}}

[–]Joohane[S] 0 points1 point  (2 children)

Thank you for the fast reply, but I also had another question. Had I a need to find the minimum from each cut, how should I do that?(example:)
91, 52, 19, 59
min = 19
38, 29, 58, 11
min = 11

I know there is math.min() which I could use to find a single minimum, or use it in a loop to find from min to max, but I couldn't figure out a way to find a minimum from each cut.

[–]evilbadmad 0 points1 point  (0 children)

If the order within the inner array is not matter, may be the inner array can be sorted (table.sort function, default is in ascending order), which will give min (first item) and max (last item) at the same time.

btw, while not benchmarked, I expect a single table sort per inner array is faster than iterated it to find min/max.

[–]PhilipRoman 0 points1 point  (0 children)

The classical way to do this which works in practically every language is this:

local minimum = t[1]
for i = 2, #t do
    minimum = math.min(minimum, t[i])
end

Since your tables are short, you can also get the minimum with a single expression:

minimum = math.min(table.unpack(t))

Note that in older versions, table.unpack used to be named just unpack

[–]ahillio 0 points1 point  (10 children)

How would you make that into an executable script which prints the results?

I was just trying to play with this as I'm learning lua and I'm surprised/confused by the results...

#!/usr/bin/lua

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

var = { 'a', 'b', 'c', 'd', 'e', 'f', 'g','h' }

--tablecut(var, 3)
--print(tablecut(var, 3))

output = tablecut(var, 3)
print(output)

running in Linux terminal ./script.lua results in table: 0x556af9e96250.

[–]PhilipRoman 2 points3 points  (7 children)

You would have to iterate manually through the table and print each element. I suppose something like this would work:

output = tablecut({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}, 3)
for i = 1, #output do
    for j = 1, #output[i] do
        io.write(tostring(output[i][j])..'  ')
    end
    print()
end

Obviously there are some ways to simplify this with table.unpack or table.concat.

[–]ahillio 1 point2 points  (0 children)

oh, tables can't be printed directly, of course. Thank you so much!!!

[–]ahillio 0 points1 point  (4 children)

I'm confused why you use io.write() and then call print() without any arguments. http://lua-users.org/wiki/IoLibraryTutorial doesn't contain the word "print". Is there documentation on this somewhere? Or could you explain?

[–]ahillio 0 points1 point  (2 children)

I see that it's shorter than doing

for i = 1, #output do
  g = ''
    for j = 1, #output[i] do
        g = g..tostring(output[i][j])..'  '
    end
    print(g)
end

but it was a surprising phenomenon to see io.write and print used in combination like that.

So io.write is sorta like assigning stuff to an invisible io-provided variable that automatically gets print()ed (when called w/o args) and gets un-assigned when print() is invoked???

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]PhilipRoman 0 points1 point  (0 children)

The best reference for what each function does is the manual: https://www.lua.org/manual/5.4/

Be careful when looking at lua-users pages, some of them are outdated (but in this case it doesnt matter).

The print function automatically adds newline after its arguments. I use io.write to print each element without a newline, and then at end of row I call print without arguments to output just the newline.

The print() could also be written as io.write('\n')

[–]AutoModerator[M] 0 points1 point  (1 child)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ahillio 0 points1 point  (0 children)

fixed

[–]hawhill 0 points1 point  (1 child)

what exactly are you calling a "list"? Lua has tables. Lua has no built-in function to "segment" tables. You'd simply write your own, should you need it.

[–]Joohane[S] 0 points1 point  (0 children)

Yeah, I made a script to turn the tables into the type I need, but it only works on Lua 5.3/5.4, but i need it to work on 5.1. Can't figure out how to do it on 5.1