Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zikku · May 22, 2013 at 04:37 PM · arraystringmultidimensional arraymultidimensional

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zikku · May 22, 2013 at 04:41 PM 0
Share

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.

avatar image Bunnybomb7670 · May 22, 2013 at 04:42 PM 0
Share

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?

avatar image Zikku · May 22, 2013 at 04:48 PM 0
Share

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!

avatar image Zikku · May 22, 2013 at 04:54 PM 0
Share

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?

avatar image Bunnybomb7670 · May 22, 2013 at 05:20 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multidimensional array variable size (JS) 1 Answer

C# String Array Has Missing or Incorrect Keycode Strings 2 Answers

Jagged Arrays vs Multidimensional 1 Answer

I don't know how to do multidimensional arrays 2 Answers

how to randomly pick a string from an array 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges