ID:892895
 
(See the best response by DarkCampainger.)
Code:
    PageSort()
var/num=1//want 1 page minimum
var/list/L = RawData
if(L.len>7)

world<<"Raw Data is [L.len]"

num=round((L.len/7)+1,1)//add a page for the rest of the values

world<<"Raw Data is rounded to roughly [num] pages"

var/maxpage=num

winset(src,null,{"
pagelabel.text = "1 of
[maxpage]";
Loader.text = "Loading....";
"}
)

var/list/x=list()// a temporary list to store data

for(var/i=1, i<maxpage+1, i++)// loops through the ammount of pages we need

x=new()//makes a new list x for us to use

for(var/j=7, j>0, j--)//loops through data and adds it to a page

if(!L[1]) return//nothing left, stop the loop

x.Add(L[1])//adds 7 things to [i] page

L.Remove(L[1])//removes them from the selection

AsocList["[i]"]=x//adds the list x the [i] page


var/i
for(i in AsocList)
world<<"Page [i] now has [AsocList[i]] items"

world<<"RawData now has [L.len] items left"


Problem description:
I'm getting a bad index runtime error when I try to run this code in game. The code is part of a system that generates pages with their own lists of items. I have looked around the forum and help files but cant find anything to explain why this error is coming up. Any help would be appreciated -Thanks
It means that you are trying to access a certain part of the list that doesn't exist. Make sure you have #define DEBUG in your code and figure out what line the error is happening on.
I defined the list like this

var/AsocList[0], judging from the manual I should be able to add values to it? Or do I have to specify how big it will be.

The error comes here:
AsocList["[i]"]=x//adds the list x the [i] page
Best response
I think your problem is more likely from this line:
if(!L[1]) return//nothing left, stop the loop

If the list is empty (size 0), trying to access the first element will throw an error. Instead, you should be doing if(!L.len) to check if it's empty.

Also, your page calculation is off. You want the ceiling value of (items)/7. Right now, if there are 14 items, you'll get 3 pages instead of 2. This should work better:
#define CEIL(_x_) -round(-(_x_))
// ...
num=CEIL(L.len/7) // add a page for the rest of the values

Basically, because round() without a second argument is the floor(), if you negate it at both ends it becomes the ceil().

Also, you may get better performance if you use list.Copy() and grab chunks of 7 items instead of doing them one by one.
Thank you for your reply Campainger, I see how using the list.Copy function would improve on the performance of the function, I was scared to use this before because the Data is sorted into an order before it is parsed through; and I naively thought Copy would not take that order into account.

As for the page calculation, I would have never guessed that it would be wrong for certain values. I will have to also read up on ceiling and floor values as I haven't seen these before.