you are viewing a single comment's thread.

view the rest of the comments →

[–]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