Very weird

Monkey Forums/Monkey Programming/Very weird

Chroma(Posted 2013) [#1]
I've come across a bug I can't squash. I'm reading in a .txt file and storing each string line in a string array.
Local quest_data:String = LoadString(quest_file)
Local line_data:String[] = quest_data.Split("~n")

In this case, line_data[13] equals the string of "1,2,3". I verified this via the Print command so all should be well. I'm sending this string to a function.
puzzle.Populate(line_data[13])   '<--Doesn't Work
puzzle.Populate("1,2,3")         '<--WORKS!!!

When I send the line_data string I get a "TypeError Cannot call Method m_Width of null". BUT if I just send a regular string of "1,2,3" the function works fine! I've guessing there's something different under the hood for a pure String and a String stored in a String array?! The weird part is I'm just accessing line_data[13] as a normal string. I just don't get it...

Any ideas?


benmc(Posted 2013) [#2]
Are you splitting the string "1,2,3" inside the Populate function?

Try adding a comma at the end of each line in your text file. So line 13 would be "1,2,3,~n" instead of "1,2,3~n" and ignoring the last position in the Populate function after the split.

Seems strange, but I had to do this with a few files in my games. I don't know exactly why, but I think it may have been because my text editor ended lines strangely.


Chroma(Posted 2013) [#3]
Yes I'm splitting "1,2,3" inside the populate function. It works when I send the straight text of "1,2,3" but not when I send line_data[13] which also equals 1,2,3.

Banding my head on the wall here...


Chroma(Posted 2013) [#4]
FOUND IT!!!

It was the .Trim() method to the rescue. Apparently something whacky gets added somewhere... (a space maybe?)

puzzle.Populate(line_data[13].Trim())



muddy_shoes(Posted 2013) [#5]
Edit: Never mind.


benmc(Posted 2013) [#6]
Yep, that's exactly what I was trying to say actually. I tack the comma on the end of all my lines in my TXT files that I import and split because I think when I split the string something stays on the end of it..... Maybe this needs to go in bugs too.


muddy_shoes(Posted 2013) [#7]
It's probably due to the text file using ~r~n EOL pairs.


c.k.(Posted 2013) [#8]
I've had a similar issue in the past. It seems, when using split, that the character used for splitting REMAINS with the split string. In other words, I get this:

x = "1~n2~n3~n4".Split("~n")

turns into

x[0] = "1~n"
x[1] = "2~n"

etc...

That doesn't seem right to me, to keep the split char with the split string, but apparently that's how monkey does it. So, I gotta trim my results (in this case).

This SEEMS to be the case. I could be misinterpreting or misperceiving the actual truth.


Beaker(Posted 2013) [#9]
I think you've got it wrong slightly. Split doesn't leave the split character at the end. It is more likely because of EOL pairs as stated by muddy_shoes, or because you sometimes end up with an extra item that is an empty string or Return character.


MikeHart(Posted 2013) [#10]
What Beaker said. Check for empty string items in your array. So far i had no problems with splitting strings.