ID:2097550
 
(See the best response by Zagros5000.)
How would I go about printing the elements of a list in reverse order? For context, I have enemies who learn moves at certain levels and want them to have moves that match the level you encounter them at (highest possible move first), rather than my current system which just gives the first 4 moves that it's possible for them to learn.

Best response
if its already in order reversing it would be easy
for(var/i=list.len;i>0;i--)
world<<list[i]
In response to Zagros5000
Zagros5000 wrote:
if its already in order reversing it would be easy
> for(var/i=list.len;i>0;i--)
> world<<list[i]
>

I don't think your answer is the best way to do it, but that's a very outdated way to do a for loop! This is less ugly.

for(var/i = list.len to 1 step -1)
world << list[i]
In response to Super Saiyan X
didn't know you could loop like that, is it faster?
In response to Zagros5000
Zagros5000 wrote:
didn't know you could loop like that, is it faster?

From my tests, yeah.
r u sure? i just did a quick test and it looks like it takes twice as long to run
Here is a slightly better version for you :P

var length = list.len
for(var/i = length to 1 step -1)
// stuff
In response to Kozuma3
Thank you (and everyone else) for the replies. But could you explain "step"? I couldn't find it in the documentation. I understand the for loop example, of course, but I'd like to know exactly what the step operator is about in case it has future application
The step operator is undocumented syntactic sugar for the increment argument of the for() loop. You can read about it, and many other things in The Red Book.
In response to Multiverse7
Multiverse7 wrote:
The step operator is undocumented syntactic sugar for the increment argument of the for() loop. You can read about it, and many other things in The Red Book.

thank you :)
All of the examples shown are just syntactic sugar for a decrement loop:

var/i = list ? list.len+1 : 1 //initializer
while(--i) //decrementor; condition
//iteration


for(var/i = list.len /*initializer*/;i /*condition*/;i-- /*decrementor*/)
//iteration


for(var/i /*initializer*/ in length to 1 /*condition*/ step -1 /*decrementor*/)
//iteration


The compiled bytecode is usually similar, but slightly different.

INITIALIZER
JUMP POINT (implied)
STATEMENT
CONDITION (if false go to implied END IF... just stored as an instruction offset)
BODY
DECREMENTOR
GOTO JUMP POINT (stored as instruction offset, not a find-goto)
END IF (implied)


The trick with the different patterns is using them results in slightly different compiled bytecode wherein you can leave out certain unneeded instructions. By using the while(--i) you are avoiding several instructions that aren't needed in the specific pattern. The problem with using the while(--i) pattern is that you are jumping over any potential niceties that the engine gives you in the for..X patterns.

For patterns remove a lot of boilerplate, so the advantage of those in most languages is that they are easier to type. Most languages auto-optimize out any unneeded instructions from these patterns so it's pointless to think about this kind of thing in the real world.