- Home /
Splitting into 2d array with JS
Hi All,
I have my highscores being returned from a php script and am able to split once on my pipe "|" character but then want to split that into a further 3 items splitting on a "," This will be so that I can layout the items in a nicely formatted manner.
I have tried using this code but it bails on compilation:
//scoreSplit = scores.Split("|"[0]);
scoreSplit arrayLists = scores.Split('|'[0]);
var allArrays : String = new string[arrayLists.Count][];
for(int i = 0; i < arrays.Length; i++)
{
allArrays[i] = arrayLists[i].Split(',');
}
}
When I run it with the first line uncommented and the rest commented it works fine but obviously its only 1d.
Any help would be appreciated as to where I am going wrong :)
regards,
mm
Answer by skovacs1 · Dec 10, 2010 at 06:54 PM
You have more problems than lines of code:
- In the uncommented version you try to use a character literal, something for which there is no syntax in Unity js - you should use a indexed character from a
String
literal like you did in the commented version. - What the heck are
scoresplit
andarrayLists
and how are they related?! If it is a variable, in unity js, you need to use the var keyword and a name, providing static type indication with a colon(:). - You statically type
allArrays
to be aString
and then assign it an array of string. string
is not a valid type in Unity js. You probably meantString
.- String[] does not have any member
Count
. Did you meanlength
? - You try to declare built-in arrays without a size and this won't work even if there was a proper syntax for 2D arrays in Unity js.
- Your for loop is checking against some undefined variable
arrays
. - Again, like 2., what the heck is
i
? See 2. for how to resolve this. - If arrays was supposed to be some kind of array, there is no member Length for Unity js arrays. Did you mean length?
- Like 1., because there is no syntax for a character literal in Unity js, this will not work. See 1. for how to resolve this.
- For lack of a proper syntax for 2d arrays, Unity js is interpreting your attempt to assign the second dimension of a 2d array of strings as you trying to assign a character to that index of a string at that index in the array. Essentially, the point is that Unity js doesn't do built-in 2D arrays unless they were declared in c#. You will need to use jagged dynamic Unity js
Array
to accomplish your goals.
Working code would look like:
var arrayLists : String[] = scores.Split("|"[0]);
var allArrays : Array = new Array();
for(var i : int = 0; i < arrayLists.length; i++)
allArrays[i] = arrayLists[i].Split(","[0])
Skovacs, To the rescue again... what would I do without you. However, to save some embarassment from the cludge of code, I should have mentioned that the code above was bastardised from some other script I had seen elsewhere so I kind of knew it wouldn't be working and as stated I was asking for some help in getting it to do its thing :) So many many thanks for the advice about arrays in Unity JS and also the code example. You are a very kind person. regards, mm
Your answer
![](https://koobas.hobune.stream/wayback/20220613090529im_/https://answers.unity.com/themes/thub/images/avi.jpg)