Hello there! I've searched on Google, Forums, etc, but i'm out of luck. I'm trying to achieve the functionality on the code below, yet I don't want to print the new line, both samples works, but not like I want: I desire to print the given string "what" into an async way, just like we do using the echo and I'm really without ideas ...
The code below prints for every millisecs:
import os
- proc wait_n_print(what: string, iterations: int, millisecs: int) =
var current = 0 var repeat = true
while repeat == true:
echo what os.sleep(millisecs)
current = current + 1
- if current == iterations:
- repeat = false
The code below prints everything at once:
import os
- proc wait_n_print(what: string, iterations: int, millisecs: int) =
var current = 0 var repeat = true
while repeat == true:
stdout.write what os.sleep(millisecs)
current = current + 1
- if current == iterations:
- repeat = false
So, again, I wanna print for every iteration the given string, and not all at once.
Any ideas? TY!
Please format your code as described in the "Code Blocks" section here.
I think you just need to write stdout.flushFile() after your stdout.write.
Also you should probably use for loops in your specific example, just in case you didn't know what they were
import os
proc waitAndPrint(what: string, iterations: int, millisecs: int) =
for i in 1..iterations:
stdout.write what
stdout.flushFile()
sleep(millisecs)
import os
proc waitAndPrint(what: string, iterations: int, millisecs: int) =
for _ in 0 ..< iterations:
stdout.write what
stdout.flushFile()
sleep(millisecs)
you made a mistake on the range
1 .. iterations is the same (and a bit cleaner, IMO) as 0 ..< iterations
You are right. Duh. I forgot the implications of the second part of my answer in the first one...
I still prefer the 0 ..< iterations as we usually iterate from zero with non-inclusive upper bound in nim. If he changes the code at some point to use that counter, my range will probably already be correct.