ID:169588
 
How do you make a list like:
List Contents
Name Amount
Candy 8
Ice Cream 10
Meat 1
I would help you but I dont understand what your asking ..
In response to Tsonic112
How would you make a list that stores a name of an item and how much you have of that item.
In response to BakaSan
Use BuzzyBoys item stacking demo.
You'd need an associative list.

mob
var
list
items = list()


mob
verb
MakeItem(item_name as text,item_amount as num)
items.Add(item_name) //Add it to the list.
items[item_name] = amount //Then set the value it's associated with.

CheckItems()
for(var/I in items)
usr << "You have [items[I]] [I]\s"
//Accessing the value is as simple as refering to it by name. (or index number if needed)
In response to Nadrew
Thanks Nadrew.
How would you check if two lists are equal?
Ex.
                var/list/A = list("Moo","Ice")
var/list/B = list("Moo","Ice")
if(A == B)
usr << "A and B are equal"
else
usr << "A and B are unequal"
if(A == null)
usr << "A is empty"
if(B == null)
usr << "B is empty"


This is what I tried but it keeps telling me it's unequal.
In response to BakaSan
How would you compare if two lists are the same?For example, if you have one list that is equal to the exact way to make cheese, and another list equal to how the user made his cheese, how would you check to see if he made his cheese correctly?
In response to BakaSan
As ineffiecient as this looks..
proc/compare(list/listA,list/listB)
//First check the obvious
if(!A&&B||!B&&A)return FALSE
if(listA.len!=listB.len)
return FLASE
else
for(var/A in listA)
for(var/B in listB)
if(A!=B)return FLASE
return TRUE

Critique this please I'd like to get more aquainted with lists.
-Intel
In response to Artificial Intel
Your way actually wouldn't work because it compares every single element in the first list to every single element in the second. In other words, your proc only returns TRUE if list_one[1] is equal to list_two[1], list_two[2], and etcetera, all the way up to list_two[list_two.len].
I would do something like this:

//returns 1 if both arguments are equal
proc/list_is_equal(list/a,list/b)
ASSERT(a && b && istype(a) && istype(b))
if(a.len != b.len) return 0
for(var/pos = 1 to a.len)
if(a[pos] != b[pos]) return 0
return 1


[edit]

That proc only compares two lists. If you want to compare more, then you would do this:

//takes an infinite number of arguments
//returns 1 if all arguments are equal
proc/list_is_equal()
ASSERT(args && args.len >= 2)
var/last = args[1]
for(var/x in args)
var/list/l = x
ASSERT(istype(l,/list))
if(l.len != last.len) return 0
for(var/pos = 1 to l.len)
if(l[pos] != last[pos]) return 0
last = l
return 1


Of course, the above proc only returns 1 if the lists being compared are in the same order. If this is not the desired behavior, then just tell me! =)
In response to Wizkidd0123
When I define a list with the user's mob...
mob
player
list
sl=list()

like this, when I try to add something to the list...
mob
kweh
verb
noname
var/num = 1
usr:sl.Add(num)
usr:sl[num] = "Chocobo"
num++

like this, I get an error...

runtime error: Cannot execute null.Add().
proc name: noname (/mob/kweh/verb/noname)
usr: Sandaime (/mob/player)
src: Sandaime (/mob/player)
call stack:
Sandaime (/mob/player): noname()

Can anyone please help?

~~The code has been edited to protect the inoccent~~

In response to BakaSan
1) Use the . operator instead of the : operator
2) mob/var/list/sl=list(), you forgot the =list() part!