Start do(loop) at 1


#1

Is it possible to start a do loop at an index > 0? I’m trying to do it another way, but I’m getting an error that idx does not exist in the current scope.

@selectedUnitList.do
   [
     if idx = 0 [skip]
     !controller : item.controller<>WaW_AI_Unit_Controller
     controller.unit_move_to_location(location)
   ]

#2

Hey Ben,
There is a special version of do called do_idx which provides the idx variable.
e.g.

!test : {1,2,3,4}
test.do_idx
[
  println(item) when idx ~= 0
]

prints
2 3 4


#3

Awesome, thank you very much!


#4

I should probably mention that it would have been more Skookum if I had done:
println(item) unless idx = 0


#5

Does :sk: have the equivalent of “continue” in loops (e.g. when a loop item does not meet certain criteria)?


#6

No, but it was planned.


#7

So how would you handle the following?

@selectedUnitList.do_idx
[
     //if idx = 0 [skip] //this doesn't work
    //Code
]

#8

I would either reverse the conditional:

@selectedUnitList.do_idx
[
     if idx ~= 0
     [
      //Code
     ]
]

Or I would first make a copy of the list with the problem entries removed. This would be situational as I’d ask myself if I really want to make a copy of the list based on its size and how often I was doing it. A simple version that makes a copy and removes the first entry:

!units : @selectedUnitList!.remove_at(0)
units.do[code]

#9

Thanks. The first approach is the one I used. I was just wondering if there was a key word, like skip, that I could use.