ID:156274
 
Hello,

I was wondering is there a way to display everything in a list as text without propagating through the list using a loop.

For example: if i had a list named Fruit and it contained fruit. declared as such:

var/list/Fruit = list("Apples", "Oranges", "Pears")

is there a way to pass the list as an argument without an index and it will print the entire list. as in:

"blah blah [Fruit] yada yada" and it would print:

blah blah Apples, Oranges, Pears yada yada.

The reason for this question is that im using a library function that takes text as an argument and if this was possible it would cut some code out.


Thanks in advance to everyone for your input,
Remaru Senata
Is it possible to read the label of multiple items in a box without opening the box and going through each item?


What you could do is encapsulate the loop in an independent proc that lets you do this 'list to text' conversion action without needing to rewrite or copy it each time you want to do it.
proc/list_items_as_text(list/X)
var/rv = "" //(initialized value isn't required)
for(var/a in X)
ry += "[X],"
ry = copytext(ry,1,length(ry)) //remove the last character (the extra ',')
return ry //return it to the caller

mob/verb/ReadList()
var/List = list("lemon", "orange", "mandarin")
src << list_items_as_text(List)


The above proc could also be rewritten in a number of other ways. You could prevent the last separator from being added in the first place, rather than removing it.

This kind of proc is in fact common and thus is included in many list and/or text-handling libraries, so you could simply include one in your project and use it, if you so desire. One such well-known library is hub://Deadron.TextHandling, but there are other good ones as well.