ID:179279
 
First, if Deadron happens to be reading this, thank you very much for such an awesome text handling library. :)

Sorry for asking such a simple question, kinda has me stumped though.

I'm trying to expand the list2text a little so it adds "and" where appropriate.

For those of you who are unfamiliar with that library, here's a snippet

dd_list2text2(list/the_list, separator)
/*
Create a string by combining each element of the list,
inserting the separator between each item.

Example:

// Turn this list of names into one string separated by semi-colons.
var/list/names = list("George", "Bernard", "Shaw")
var/separator = "; "
var/mytext = dd_list2text(names, separator)
*/
var/total = the_list.len
if (total == 0) // Nothing to work with.
return

var/newText = the_list[1]
var/count
for (count = 2, count <= total, count++)
newText += separator
newText += the_list[count]
return newText


What I tried doing was adding a if (total == 2) separator = " and " riht under the if (total == 0), this returns a statement has no effect thing, so I'm convinced it has to go in the for() somewhere. Unfortunately, I have no clue at all what all that stuff in the for() means (count++?). Can anyone help explain it a little bit better? The only thing I've ever used for() for is locating objects and mobs and stuff.
Zagreus wrote:
for (count = 2, count <= total, count++)
newText += separator
newText += the_list[count]
return newText


What I tried doing was adding a if (total == 2) separator = " and " riht under the if (total == 0), this returns a statement has no effect thing, so I'm convinced it has to go in the for() somewhere. Unfortunately, I have no clue at all what all that stuff in the for() means (count++?). Can anyone help explain it a little bit better? The only thing I've ever used for() for is locating objects and mobs and stuff.

This form of a for loop is exactly like C, in case you discover that later on. There are three parts to it: initialization, the test for whether the loop should continue, and what the loop should do after each iteration. In the above code, that means:
  start the loop with count = 2
before each iteration, continue the loop only if count <= total; otherwise end it
at the end of each iteration increment count (count++)

Almost every for loop you see will do something very similar. Initialize the "loop" variable to the start number, test to make sure it isn't bigger (or smaller) than the end number, and increment (or decrement) it each time. There's plenty of other things you can do with it, but this is by far the most common.

As for what you want to do: I'd suggest ending the loop one time early (you could do so by testing for count < total instead of count <= total) and then add the last list element separated by "and" after the loop. See what you can do with that. Remember you'll need to handle the case when there's only one list element (no "and" or separators needed).
Zagreus wrote:
First, if Deadron happens to be reading this, thank you very much for such an awesome text handling library. :)

Thanks. Looks like Mapster covered the details, so I'll just mention I'll probably be expanding the library soon. I think I have about 5 functions spread around in my code that belong there...the MapText library inspired me to create a couple more too.
In response to Air Mapster
Thanks... I can't believe you can use for() for something other than searching the world list. :)

I tried editing it a bit, and got some errors, and am confused about some other things.

If you look at line 206 in the pasted stuff below, you'll notice Deadron adds part of the list to the new string with

newText += the_list[count]

This works in the game, but in my head it would seem it would just display thelist1 or thelist2, and on and on. How's that work?

So, going off of that, I just added another variable called finalseparator (just incase I ever decide to use or or something like that) and... well, the results are below. A very confusing error. Really giving me a headache.


runtime error: type mismatch
proc name: dd list2text2 (/proc/dd_list2text2)
source file: TextHandling.dm,207
usr: Zagreus (/mob)
src: null
call stack:
dd list2text2(/list (/list), ", ", ", and ")
Floating in a void (/room): north()
Zagreus (/client): North()

I have a few questions about this.
What's a type mismatch error? :)

it's saying src = null, yet I don't use src anywhere in there, so why is it relevant?




For those of you who are curious, heres everything thats mentioned in the call stack thingy.

dd_list2text2(list/the_list, separator, finalseparator)

var/total = the_list.len
if (total == 0) // Nothing to work with.
return

var/newText = the_list[1]
var/count
for (count = 2, count < total, count++)
newText += separator
newText += the_list[count]
newText += finalseparator//line 207
newText += the_list[count]
return newText


room/proc
DoAction(name)
var/action/a
for(a in src)
if(a.name == name) a.Action()
north()
if(usr.Move(north_exit))
src << "[usr] leaves to the north."
usr << "\[[usr.loc]\]"
usr << usr.loc.desc
var/tmp/whoinroom = list()
var/tmp/mob/M
for(M as mob in view())
if(M != usr)
whoinroom += M
var/tmp/separator = ", "
var/tmp/finalseparator = ", and "
var/tmp/textwhoinroom = dd_list2text2 (whoinroom, separator, finalseparator)
usr << "Also here: [textwhoinroom]"
oview(usr) << "[usr] arrives from the south."
else DoAction("north")


If it looks familiar to you, it's from Dan's mud engine, really awesome, but I was trying to get it to display everything in the text window instead of the stat panels and really messed it up. :)

When the user moves north to an empty room, all is well, when a mover uses north to an occupied room, that error pops up. Any suggestions to what's causing it? I don't even now how to start tracking a problem like this.