ID:1806628
 
(See the best response by Lummox JR.)
Code:
Problem description:
This is relatively simple , however I can't find a proper example of it being done anywhere, and I can't seem to figure out how to do it so I thought I'd ask :

I'm trying to loop through a list, but only certain parts of the list.
For instance :

var/list/a=list("A","b","l","m","h")
//then only loop through indexes 1-4


For some reason I can't use 'to' properly to do this, but I may be approaching it wrong.

I could just copy the part of the list I want to loop through and do so, but it seems as if this above method should be possible.
IIRC, you can just use a for loop.
var/list/a=list("A","b","l","m","h")
//then only loop through indexes 1-4

for(var/x = 1 to 4)
world << a[x]
In response to Lige
Lige wrote:
IIRC, you can just use a for loop.
> var/list/a=list("A","b","l","m","h")
> //then only loop through indexes 1-4
>
> for(var/x = 1 to 4)
> world << a[x]
>


The issue is that I'm looping through a list of objects to display in a shop.

I need to only display x amount of items at a time to accommodate the shop window.

Clicking an arrow displays page 1, page 2 and so on, each page shows more of the inventory.

I tried what you just mentioned above, but it doesn't seem to work.
Best response
// round up page count
var/n_pages = -round(-items.length / per_page) // -round(-x) is equivalent to ceil(x)
var/start = (page-1) * per_page
var/end = min(start + per_page, items.length)
for(var/i = start, i < end, ++i)
player << items[i]

The n_pages value doesn't factor into this loop, but it can be useful when displaying page numbers.
// -round(-x) is equivalent to ceil(x)

...Hm... Never thought of doing it that way. Definitely faster than my current approach... Le steal.