Hey guys
Having a very frustrating problem right now when trying to split a string into arrays and loop through them properly.
I’ve got one long delimited string that is being passed by the browser:
var codString : String = "10000|Leigh|Share|http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs446.snc4/49150_511074772_6638_q.jpg~79813|Evil Leigh|Share|http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs446.snc4/49150_511074772_6638_q.jpg";
So I’ve got two different users in there, delimited by a ~, and then each individual user’s info is delimited by |.
At the moment, my function to split them into individual arrays looks like this:
function returnFriends (str) {
var playerArray : Array = str.Split("~"[0]);
print(playerArray.length); // Correctly returns 2
for (var a = 0; a < playerArray.length; a++) {
var playerString : String = playerArray[a];
var playerSplit : Array = playerString.Split("|"[0]);
print(a + ") Length: " + playerSplit.length); // Correctly prints the array index and length, i.e. 0 4
for (var j = 0; j < playerSplit.length; j++) {
print(j + ") " + playerSplit[j]); // Works first time, fails halfway through second iteration
}
}
}
This all seems to fall apart in the embedded for loop the second time around. The first time (on playerArray[0]) it works perfectly; I get all four elements of the array. The second time around, even though it gives me the correct array length for playerArray[1], it stops at playerSplit[1].
I’ve tried coding this about 5 different ways, with the same result every time. I’m very handy with AS3, so I copied, pasted and ran this function in Flash (with minor code tweaks to make it actually work) just to make sure I wasn’t missing something fundamental, but it works exactly as expected in Flash.
Can anyone tell me what I’m doing wrong? I have noticed that if I use a much shorter intial string, it seems to work. Is this a limit of String (or Split)?
I’ve also noticed that every time I compile my game, if I’ve changed my string values, I have to change the name of my initial string (hence why it’s presently called codString) otherwise Unity caches it.