String.Split() / Array length issues and cached strings... oh my

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.

…you know what, I think I’ve figured it out (although it doesn’t really make much sense):

If any element of the array you’ve created is the same as a previous array iteration, Unity actually stops processing it. Huh?

For the most part this doesn’t seem like a major issue, but if any users have the same last name (i.e. ‘Smith’) then Unity will just stop. Does anyone know any way around this?

No, it’s just that any public variables take their values from the inspector, not your code.

That’s something your code is doing, not Unity.

–Eric

Perhaps I’m approaching this entirely incorrectly, then. As I said, I come from a very strong AS3 background, and this function works exactly as I would expect in Flash, but not even close in Unity. When you said:

…do you have any idea what my code is doing wrong? I’m assuming it’s something to do with scope, but it seems like scoping is quite different in UnityScript from AS3.

EDIT

Ignore this post. I’m not sure what was initially throwing the error that I was getting (about an undefined Array access) since I recoded this thing from the ground up about 19 times, but I’ve only just realised I had the debug window’s ‘collapse’ turned on, so any repetitive iterations weren’t getting traced. It appears I’ve been banging my head against a wall for hours for no reason.

Oh, well - at least now I truly understand the meaning of the acronym ‘FML’ :confused:

You are looking for a script that will split strings into an array? Use an accumulator to gather the data you are working on, verify it against the separator and push the completed stuff onto a stack.

OK, that sounds a little complicated, Splitting strings is a simple process when you start with the string as a unit. Like so:

function Start(){
	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";  
	print(Split("This is a test", " "));
	print(Split(codString, "~"));
	var codes=Split(codString, "~");
	for(code in codes)print(Split(code,"|"));
}

function Split(sStr : String, sSep : String){
	var acc="";
	var xit=false;
	var p=0;
	var ret=new Array();
	while(xit==false){
		if(p==sStr.length){
			ret.Push(acc);
			acc="";
			xit=true;
		}else{
			if(sStr[p]==sSep){
				ret.Push(acc);
				acc="";
			}else{
				acc+=sStr[p];
				if(sSep==""){
					ret.Push(acc);
					acc="";
				}
			}
		}
		p++;
	}
	return ret;
}