ID:1718838
 
(See the best response by Kaiochao.)
Code:
for(var/i in mylist[1 to min(mylist.len,4)])
world<<i


Problem description:
I was wondering, say I have a list like so :
list/mylist=list("a","b","c","zed")


How would I loop through certain values within the list?

Like say loop through 1-3, or 2-4 for instance?
I've tried setting up a for loop in various ways but it doesn't seem to be working out for me.

The above is just an example expressing what I want, not even sure if it compiles correctly in that example. But I did try something very much like that with no results.
There's always the original for loop syntax:

for (var/i = [begin]; i <= [end]; i += [increment])


This should give you however flexibility you'd like.

To iterate through a list (As an example):

var/list/l = new/list(10)
for (var/i = 1 to 10)
world << l[i]
Best response
You could use a for loop:
for(var/index in [start] to [end] step [increment])
// or
for(var/index in [start] to [end]) // increment = 1

// So if you wanted to loop through the 2nd-4th item in a list,
for(var/index in 2 to 4)
var item = a_list[index]
// now [item] is the [index]th item in [a_list]
src << item


You could also use list.Copy():
var a_list[] = list(...)
for(var/item in a_list.Copy(start, end+1))
src << item

for(var/item in a_list.Copy(2, 5)) // from 2 to 4
Thanks you two, very helpful.