ID:154817
 
Literally have gotten nowhere in the past 2 weeks now.

How I define a list within a list:

mob
var
list
item_list = list()
verb
define_list()
for(var/obj/o in contents)
if(!(o.name in item_list))
item_list[o.name] = list()
item_list[o.name] += o


The ONLY way that works to access it:

mob
verb
check_list_1()
for(var/i in item_list)
world<<i
for(var/n in item_list[i])
world << "found item in list"


A way that SHOULD work and I need it to:

mob
verb
check_list_2()
world << "found [item_list[1][1]]"
You will need to define another list then and add the item in the second /list if you want it to work that way.

The reason why it isn't working as you expected is due to the following:

list[N] --> returns the o.name here (generally, returns the Nth entry in the /list)

list[N][M] --> Will return the Mth entry in a /list of the Nth element.

The above is assuming that M & N are numbers

If you enter the reference (or string in this case), it'll return its value:
list[o.name] --> returns the object that it has stored as its value




The point is, if you want to access the object with the system you have set up now, you need to do:

item_list[item_list[1]]
(the one inside the [] will return the reference and the whole thing will return its value)

If you want it the [N][M] method, then change:
 item_list[o.name] += o
TO
item_list[o.name] += list(o)





Summary:
/list = list(Entry = Value, "X" = Y)

list[Number] == returns the Entry value for that position in the list

list[Entry] == returns the value that has been set

list[list[Number]] == list[Entry]
In response to GhostAnime
Thanks for your time and detail - appreciated. I'll look into it soon!
In response to Speedro
No problem. I remember when I tried learning about arrays/lists a few years ago, so confusing... though I look back now and say "Really? I had a problem with THIS?!"

Good times, lots of frustrations but good times. If you need further clarifications, do not hesitate to post ^_^
In response to GhostAnime
Ok, I narrowed it down, but to still use it I need to use a for() loop to access the contents - I tried indexing instead but still. Can I simplify this?:

                for(var/i in item_list[item_list[y]])
world << "choice: [i]"
choice = i
break


Also do you have msn/skype? I used to have you on msn a few years back, if you recall trying to make a fishing game with me. :p

EDIT:

Also another question: How can I have a for() loop list through only a certain amount of a list? E.G.

var/list/test = list("banana","apple","orange","cucumber","zebra","rabbit","chimp","beer","laptop","programming")

mob
verb
test_loop()
for(everything from test[4] to test[7])
world << "this was found!"


EDIT: Nevermind, fixed the second issue with a startpoint/endpoint var.
In response to Speedro
Speedro wrote:
Ok, I narrowed it down, but to still use it I need to use a for() loop to access the contents - I tried indexing instead but still. Can I simplify this?:

                for(var/i in item_list[item_list[y]])
> world << "choice: [i]"
> choice = i
> break


choice = item_list[item_list[y]][1]
world << "choice: [choice]"
In response to Jemai1
Yup that works, but the index inside index is confusing.
In response to Speedro
Okay, I'll explain it to you a bit.

For example we have
var/list/TheList = list("item1"=list("item1.1","item1.2"),"item2"=list("item2.1","item2.2")

//here we have
TheList[1] == "item1" // <--
TheList[2] == "item2"

//and
TheList["item1"] == list("item1.1","item1.2") // <--
TheList["item2"] == list("item2.1","item2.2")

//therefore,
TheList[ TheList[1] ] == TheList[ "item1" ] // refer to the line with the 1st arrow
//and
TheList[ TheList[1] ][1] == TheList[ "item1" ][1] == "item1.1" // refer to the line with the 2nd arrow