Hi
I was trying to reverse a string using a sequence and high to low iteration, but it does not output anything for some reason. When using the same iteration going from low to high it works as expect.
Not sure what I am doing wrong - is this not supposed to work, or am I doing something dumb (again!) :)
Thanks
Simon
proc reverse2() =
let seqStr = @"test123"
echo "Sequence:", repr seqStr
echo "High:", high(seqStr)
echo "Low", low(seqStr)
echo "Low to high..."
# works...
for i in low(seqStr)..high(seqStr):
echo seqStr[i]
echo "High to low..."
# does nothing...?
for i in high(seqStr)..low(seqStr):
echo seqStr[i]
echo "App running"
reverse2()
echo "DONE"
The reason it doesn't work is that N .. M in the context of a for loop implicitly calls countup.
You need to explicitly call countdown:
for i in countdown(high(seqStr), low(seqStr)):
echo seqStr[i]
Thanks Vindaar.
I saw countup and countdown in the Nim documents, but (wrongly) thought using high and low would provide the same. Especially as the low..high works fine without the use of the extra countup.
Thanks for the help and the example correct code usage.