- Home /
Newbie Problem with Multidimensional Arrays in JavaScript
I recently wrote code to import and parse data in AS3 from a CSV file. My psucdo-code
Each row of the CSV file has it's individual elements pushed into a new array.
That array is then pushed into an array, creating a multidimensional array.
Because the length of each row, and however many rows there are are completely dynamic and depends on what my client enters into the CSV file, I have no way of knowing the total amount (and it's not a perfect square) until the information has been imported and parsed.
This is what I have so far:
static var myDataString:String[];
static var myData:Array = new Array();
function LoadData()
{
var dataStream:StreamReader = new StreamReader('SessionData.csv');
var dataContents = dataStream.ReadToEnd();
dataStream.Close();
//This is a string that has organized the data into the appropriate rows, but now we need the correct columns too.
myDataString = dataContents.Split('\r\n'[0]);//Splits Data into Appropriate Rows
//print('What is in my Data String? Let us find out!!');
for(var i:int=0;i<=myDataString.Length-1;i++)
{
//print('---');
//print('This is Row '+i+' of the Data String:');
//print(myDataString[i]);
//Sort Rows into their Columns
var rowArray:Array = new Array();
rowArray = myDataString[i].Split(','[0]);
//print('Entire Row: '+rowArray);
//print('First Item in Row: '+rowArray[0]);
//Place the entire row array back into the larger data array to make a multidimensional array
myData.push(rowArray);
}
print('Data Length: '+myData.length);
//print('Row 0 Length: '+myData[0].length);
print('Entire Data:');
print(myData);
print('--- End Data ---');
print('3rd Row in Data:');
print(myData[2]);
print('2nd Element in 3rd Row in Data');
print(myData[2][1]);
}
And here is an example of what prints out from this madness:
Data Length 7
Second Row Length: 18
Entire Data:
Session ID Number,Grid Size (0= Previous Grid Size),Number of Trials,Number of Targets (Must be less than the Number of Trials),Number of L Blocks per Trial (Must be less than the Grid Size squared),Movement Rate,Scale Rate,Start Position,End Position, 1,3,3,1,2,1,1,0,0, 2,3,3,2,3,1,1,0,0, 3,3,3,1,5,1,1,0,0, 4,3,3,1,4,1,1,0,0, 5,3,3,1,3,1,1,0,0,
--- End Data---
3rd Row in Data:
2,3,3,2,3,1,1,0,0
2nd Element in 3rd Row in Data
Then finally, the last line throws an error "BCE0048: Type 'Object' does not support splicing." (in fact, the program won't run because of this error). And that's the friendliest error I've received after several methods. I am not keen on how the String[] variable type works. I'm not really even sure of what it is. I only found it working in other threads that were similar to my problem, and was using trial and error to get it working.
In the other languages (python and AS3), I can access a single element in a multidimensional array by using that line myData[2][1], but obviously the JS in Unity doesn't like it. If I know the index numbers in my arrays (essentially the row, then column number), how can I access the element?
P.S. The print statements that are commented out inside the for-loop were to prove that each row was separated into the elements in an array before being pushed into my big, master array.
Thanks!
Wow, terrible formatting in the "block comment" thing (let me try to fix that).
Anyway, the elements in the first row are just the "titles" for the client to use, and I won't be acknowledging them later on.
Cant you possibly get the longest length of each direction and make an array of the longest x by longest y and set the unnecessary values to 0 / -1 and ignore them when reading through it?
Can you show an example? Again, I'm only familiar with creating blank arrays and then tossing things in them willy-nilly. I've not had much experience with these String[] or List variables (if you're even talking about those, I don't know...). I'm ashamed to call myself a programmer!
So I'm looking at this page , specifically the 2d "jagged arrays". It looks like the perfect solution that I could use, but in the example, they're using a 16x4. I don't have a way of predeter$$anonymous$$ing that information until after. Any suggestions?
var dataArray int[,] = new int[X,Y]; // 2D Array var dataArray int[,,] = new int[X,Y,Z]; // 3D Array var dataArray int[,,,] = new int[X,Y,Z,W]; // 4D Array
These however are limited to a specific type. Jagged are for multi type. I suggest having an integer, starting at 0, and increasing during the search for data, and being the longest length of the data. its kind of hard to explain.