ID:145356
 
Code:
mob/var/shop = list()   

mob
verb
Shop()
var/item = input("Select an item you would like to add to shop.","Shop") in src.contents
var/price = input("How much would you like to sell [item] for?","Shop") as num
shop += item
shop += price
var/A
for(item in shop)
A ++
world << "Item: [shop[A]], Price: [shop[A+1]]"
A ++


Problem description:
K well It keeps on telling me that list index is out of bounds when I run the program and do the verb.. The reason probably being that it refers to shop[3] the second time it loops in the for statement . Even though it gives me this error it displays what I want it to.

I dunno why it wont let me refer to shop[3] since im adding stuff to the list that would make the list bigger then 3. Also when i tried declaring the list as mob/var/shop[20]. When it got to the for loop it displayed "Item: , Price: " 19 times before it showed the actual item and price.

Any help would be greatly appreciated.

Well, one obvious problem here is that you're looping for(item in shop), which will literally loop through all items in the list--both objects and prices. Of course, why you didn't use an associative list, which is infinitely better, is a mystery. But be that as it may, your loop is covering the entire length of shop, and yet is incrementing A twice each time through. By the time you're done, A will be shop.length*2. As soon as A reaches shop.length or higher, you'll get a runtime error because shop[A+1] will go past the end of the list.

Solution: Use an associative list. Ditch the A var entirely.

Lummox JR
In response to Lummox JR
Lummox JR wrote:
Well, one obvious problem here is that you're looping for(item in shop), which will literally loop through all items in the list--both objects and prices. Of course, why you didn't use an associative list, which is infinitely better, is a mystery. But be that as it may, your loop is covering the entire length of shop, and yet is incrementing A twice each time through. By the time you're done, A will be shop.length*2. As soon as A reaches shop.length or higher, you'll get a runtime error because shop[A+1] will go past the end of the list.

Solution: Use an associative list. Ditch the A var entirely.

Lummox JR

I tried reading up on associative lists and didnt quiet understand it as all I reached were problems when I tried making one. If you could show me an example of an associative list that would be very helpful.

In response to X0Gohan0X
X0Gohan0X wrote:
I tried reading up on associative lists and didnt quiet understand it as all I reached were problems when I tried making one. If you could show me an example of an associative list that would be very helpful.

shop[item] = price

Lummox JR
In response to Lummox JR
Lummox JR wrote:
X0Gohan0X wrote:
I tried reading up on associative lists and didnt quiet understand it as all I reached were problems when I tried making one. If you could show me an example of an associative list that would be very helpful.

shop[item] = price

Lummox JR

Thanks a bunch, I finally feel comfortable with lists now.