How to print without skipping a line in Nim
Asked Answered
M

2

5

I would like to print without skipping lines in Nim.

this is my code by far

int i = 1
for i in countup(1, 10):
  echo "number: "
  echo i

I would like the output to be:

number: (i value)
...

instead of:

number:
(i value)
Miller answered 9/8, 2020 at 11:12 Comment(0)
P
2

Convert i to a string with the $ operator, then concatenate the strings using the & operator.

echo "number: " & $i
Philo answered 9/8, 2020 at 11:58 Comment(2)
You can simply echo "number: ", i, without concatenating and forcing conversion. In my opinion, this leads to cleaner code.Custody
Compared to the other answer, this is the way to go if you just want to output strings.Senghor
S
6

You can also write to stdout directly with

stdout.write "number: "
echo i

which will print a newline after printing the value of i

Sandrocottus answered 5/4, 2022 at 20:31 Comment(1)
Compared to the other answer, this is the way to go if you want a REPL input: stdout.write "insert something\n> "Senghor
P
2

Convert i to a string with the $ operator, then concatenate the strings using the & operator.

echo "number: " & $i
Philo answered 9/8, 2020 at 11:58 Comment(2)
You can simply echo "number: ", i, without concatenating and forcing conversion. In my opinion, this leads to cleaner code.Custody
Compared to the other answer, this is the way to go if you just want to output strings.Senghor

© 2022 - 2024 — McMap. All rights reserved.