[Solved] Help: repeat a function "x" times

What is the correct code for repeating a function or a line of code “x” times?

For example:

x = 10 (or any variable number obtained)

repeat the function

function AAA()

–content

end

“x” times

Then, AAA() is repeated 10 times.I have searched the code in the lua manual, but I can not get it to work.

or repeat inside the function:

function BBB()

x = 5

–a “L” line, for example: song.insert_track_at(1) --insert a track

–repeat line “L” line x times

end

Then, the “L” line is repeated 5 times inside the function BBB()

What is the exact codes?

Thanks for the help!

1 Like

a while loop ? Dunno if I understand the question correctly…

while [LOOP MATCHING CONDITION == TRUE] do
 bleh
  ATTENTION: Loop matching condition must change to false inside at some point, or while will loop forever. Or use a "break" statement.
end

Or a for loop, which is capable of increasing a counter by itself.
for [START COUNT],[LOOP MATCHING CONDITION == TRUE],[COUNTER CHANGE] do
bleh
end

for [START COUNT],[COUNTER END VALUE],[COUNTER CHANGE] do
 bleh
end

For assoziative arrays/object-containers (in LUA “tables”) you can use

for key IN table do
 bleh
  print(table[key])
end

Or if you want to have directly access to the value:

for key,value IN pairs(table) do
print(key,value)
end

Have a look into the standard LUA documentation for examples…
http://lua-users.org/wiki/ForTutorial
https://www.tutorialspoint.com/lua/lua_while_loop.htm

I think normally you would not get an answer here asking such a basic programming questions, because the devs expect you do your base homework.

With " for … do":

function repeat_x4(song, a) --repeat 4 times with--> for do
  song = renoise.song()
  a = 4
  for i = 1, a, 1 do
    song:insert_track_at(song.selected_track_index)
  end
end

Is there any way to do the same with " repeat" and " until"?

Hm, sorry. I think I mixed up things (again), cause lua-for-loops are different to other languages, kinda special :X. Will correct† above.

Yes looks right to me. But don’t know for sure without testing.

I did not even know repeat statement, but looks for me like a workaround of lua, due lack of using do-while for this. In other languages:

do
 bleh
while ([matching condition])

So in lua that thing with repeat. The diff to normal while is that it will process the inner code at least one time for sure.

In your case:

function my_personal_track_insert(num)
 local song = renoise.song()
 local i = 1
 repeat
  song:insert_track_at(song.selected_track_index)
  i = i + 1
 until i > num
end
...
my_personal_track_insert(4)

Or in combination with while:

function my_personal_track_insert(num)
 local song = renoise.song()
 local i = 1
 repeat
  song:insert_track_at(song.selected_track_index)
  i = i + 1
 while not (i > num) -- or while i <= num
end
...
my_personal_track_insert(4)

So basically, with until you define the condition when to stop, and with while the condition when to loop. Though it does not seem to make sense here to me to force one iteration. And I personally like the repeat-while variant much more, because it’s basically the same as a normal while.

Heh, maybe you should ask a lua geek in here instead, and I’ll shut up now. I also am a LUA hater.

With " for … do":

function repeat_x4(song, a) --repeat 4 times with--> for do
song = renoise.song()
a = 4
for i = 1, a, 1 do
song:insert_track_at(song.selected_track_index)
end
end

Is there any way to do the same with " repeat" and " until"?

Errr…maybe?

function repeat_x4(song, a) --repeat 4 times with--> repeat until
  song = renoise.song()
  a = 1
  repeat
    song:insert_track_at(song.selected_track_index)
    a = a + 1
  until a > 4
end

Hm, sorry. I think I mixed up things (again), cause lua-for-loops are different to other languages, kinda special :X. Will correct† above.

Yes looks right to me. But don’t know for sure without testing.

Thanks ffx for the help! :slight_smile:

I am simply looking for a simple function to repeat, with repeat and until. For example (with os.clock):

local starttime = os.clock()
print("I'm about to spam you horribly for 1.5 seconds")
repeat
	print("SPAM!")
until os.clock() - starttime > 1.5
print("All done :P")

Very interesant! Repeat a function 1.5 seconds.Instead, I want to repeat only 5 times

Possibly repeat and until does not work, and I must use for and do.

Errr…maybe?

function repeat_x4(song, a) --repeat 4 times with--> repeat until
song = renoise.song()
a = 1
repeat
song:insert_track_at(song.selected_track_index)
a = a + 1
until a > 4
end

jajajaImpressed with speed!!! Perfect and solved!!! Taaaaaaanks 4Tey!!!

The functions:

function amic_CH04_5B(song, a) --repeat x4 with "repeat until"
  song = renoise.song()
  a = 1
  repeat
    song:insert_track_at(song.selected_track_index)
    a = a + 1
  until a > 4
end

function amic_CH04_6B(song, a) --repeat x4 with "for do"
  song = renoise.song()
  a = 4
  for i = 1, a, 1 do
    song:insert_track_at(song.selected_track_index)
  end
end

function amic_CH04_6B(song, a) --repeat x4 with "for do"
  song = renoise.song()
  a = 4
  for i = 1, a do
    song:insert_track_at(song.selected_track_index)
  end
end

Any idea with vertical scrollbar ? :https://forum.renoise.com/t/solved-help-create-an-area-with-vertical-scrollbar-with-buttons-in/47059

If you use a for-loop, and you do not use the variable inside the loop, you should use the underscore as counter to avoid luacheck warnings:

local a = 4
for _ = 1, a do
  song:insert_track_at(song.selected_track_index)
end

What warning do you mean?
With the current API version and with the current LUA version, with Renoise, it doesn’t matter which name precedes behind the “for”. It will not return any errors or any warnings. What is the problem here?