ID:154296
 
What I need to do, I've got a dozen lists, and a for() check for everything in each list. But, I need it to return a different responce if there's nothing in any of the lists.

Original idea works like this:

mob/proc/checklists()
usr << "Your lists contain:"
var/check = 0
for(var/O in list1)
usr << O
check = 1
for(var/O in list2)
usr << O
check = 1
for(var/O in list3)
usr << O
check = 1
for(var/O in list4)
usr << O
check = 1
for(var/O in list5)
usr << O
check = 1
for(var/O in list6)
usr << O
check = 1
for(var/O in list7)
usr << O
check = 1
if(!check) usr << "Nevermind, there's nothing here."


So, now, I'm looking for ways to improve this, since this is really sloppy looking!
I suggest this:
mob/proc/checklists()
var/list/masterlist=list(list1,list2,list3,list4,list5,list6,list7)
var/list/curlist
var/items=0
for(curlist in masterlist)
for(var/O in curlist)
if(!items) usr << "Your lists contain:"
usr << O
++items
if(!items) usr << "Your lists are empty!"

This falls apart if you have to check for different types, like var/obj/weapon/O and var/obj/food/O, though there might be other ways to deal with that.

Lummox JR
Foomer wrote:
What I need to do, I've got a dozen lists, and a for() check for everything in each list. But, I need it to return a different responce if there's nothing in any of the lists.

I think this could just be done using a list of lists. I haven't played around with multi-dimensional lists in DM much, so I'm not sure if this would be the exact syntax, but something like

var/all_lists[] = list(list1, list2, list3 ...)
for (var/list/L in all_lists)
for (var/O in L)
usr << O
check = 1
In response to Lummox JR
Lummox JR wrote:
I suggest this:
mob/proc/checklists()
> var/list/masterlist=list(list1,list2,list3,list4,list5,list6,list7)
> var/list/curlist
> var/items=0
> for(curlist in masterlist)
> for(var/O in curlist)
> if(!items) usr << "Your lists contain:"
> usr << O
> ++items
> if(!items) usr << "Your lists are empty!"

This falls apart if you have to check for different types, like var/obj/weapon/O and var/obj/food/O, though there might be other ways to deal with that.

Yep... BYOND supports adding of lists, which would be a good way to do that, I think.

var/list/masterlist = list1 + list2 + list3 + list4 + list5 + list6 + list7

Then masterlist is just a list of everything.