ID:266327
 
This is a proc I created to handle some text situations in my text engine:
proc/text2list(var/text = "" as text)
var/length = length(text)
if(length > 0)
var/list/charlist = list()
var/charlistnum = 1 as num
var/char = "" as text
while(length)
char = copytext(text,1,2)
charlist[charlistnum] = char
charlistnum += 1
text = copytext(text,2,0)
length -= 1
return charlist
else return list()

This proc is supposed to return a list of each piece of the text string inputed. So, I set up a test:

mob/verb/test()
var/test = text2list("test")
world << "test[1] == [test[1]]\nIt should be == t"


However, I get a runtime error when I run test() that looks like the following:

runtime error: list index out of bounds
proc name: text2list (/proc/text2list)
usr: Lord of Water (/mob)
src: null
call stack:
text2list("test")
Lord of Water (/mob): test()
runtime error: cannot read from list
proc name: test (/mob/verb/test)
usr: Lord of Water (/mob)
src: Lord of Water (/mob)
call stack:
Lord of Water (/mob): test()

Can you help me figure out how to eliminate this error?
Lord of Water wrote:
Can you help me figure out how to eliminate this error?

Try the TextHandling library, which handles this for you with the dd_text2list() function.

byond://Deadron.TextHandling
In response to Deadron
Oh my... and I thought my name would be origional! I still want to code it for myself, but I'll take a look.

[edit] Looking at the text handeling library did not help me a lot. Could you attempt to explain why it works how it does and/or how to fix my proc?
In response to Lord of Water
Lord of Water wrote:
[edit] Looking at the text handeling library did not help me a lot. Could you attempt to explain why it works how it does and/or how to fix my proc?

All the code is right there, and it's only a few lines, which are all commented as I recall. If you have a specific question let me know...but since that solves the entire thing in about the only way it can be solved, I don't have much to add.
In response to Deadron
I can see the code, but it is hazy to me as to just what it does. I see a list, but you use += to add something to it. When I try to do that, it does not add a new item to the list, but adds to an old item. As for your comments, they are not helpful. When you are commenting code, think of your comments this way: I can read DM just fine, but do not know -why- you are doing what you are doing with the code. Comments should explain reasoning, not just restate what the code stated.

while(1) // loop forever
textList += "" // add empty element

Comments like these are not helpful.

Any way, I want to see how to do what I want to do without seperators or buggytext or anything of the sort. I want to do it the way above, but change the way above so that it does not generate errors.

Thanks!

-Lord of Water
In response to Lord of Water
Lord of Water wrote:
Any way, I want to see how to do what I want to do without seperators or buggytext or anything of the sort. I want to do it the way above, but change the way above so that it does not generate errors.

Sorry don't have any answers for you...can't do better than debugged working code as an answer!
In response to Deadron
Okay... instead of answering the question: how do I turn a text string into a list, attempt to answer the question: how do I illiminate the list index out of bounds runtime error? I really want to learn how to code better, and knowing what is wrong with my code seems like a good way to go about it. My code does exactly what I want it to do, in theory. However, it generates an error that I can make nothing of. Again, thanks for your time in helping me.
First of all, some general concept stuff:

You are using "as num" and "as text" in places where they don't do anything. The only time that works is in the declaration for a verb...everywhere else it is meaningless. So you can just delete all the highlighted sections.


proc/text2list(var/text = "" as text)
var/length = length(text)
if(length > 0)
var/list/charlist = list()
var/charlistnum = 1 as num
var/char = "" as text
while(length)
char = copytext(text,1,2)
charlist[charlistnum] = char
charlistnum += 1
text = copytext(text,2,0)
length -= 1
return charlist
else return list()

Next, what line is the error occurring on? You should add:

#define DEBUG

to your project so you get line numbers in the errors.

Then I'd recommend adding some debug statements here and running through the test...for example, output the value of text and char and length each time through the list, so you can see what's happening.

My guess is that it's this line:

charlist[charlistnum] = char

You can't use charlistnum to extend the size of the list, and you didn't set a size for the list up front...it should work if you use one of these two approaches:

charlist += char

or

charlist.Add(char)

Both of which do exactly the same thing: Add the character as a new element in the list. You can just get rid of charlistnum.
In response to Deadron
Aha. Thank you very much, Deadron!