Search Unity

Another Array q

Discussion in 'Scripting' started by yellowlabrador, Aug 27, 2007.

  1. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Hi All,

    Just wanted to know if this is correct

    Code (csharp):
    1.  
    2. var arr = new Array(195);
    3. arr = new Array('abc","def","ghi".....upto "195");
    4.  
    or I have to do it this way
    Code (csharp):
    1.  
    2. var arr = new Array();
    3. arr.length = 195;
    4.  
    5. var abc : GameObject;
    6. var def : GameObject:
    7.  
    8. all the way down to 195...
    9.  
    10. arrABC[0] = "A";
    11. arrDEF[1] = "B";
    12.  
    13. all the way to 195;
    14.  
    Thanks,
    Ray
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I find this to be the easiest:


    Code (csharp):
    1.  
    2. var arr : Array = ["abc","def","ghi".....upto "195"];
    3.  
    Or possibly like this:

    Code (csharp):
    1.  
    2. var arr = new Array();
    3. arr.Push("abc");
    4. arr.Push("def");
    5. // etc.
    6.  
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Just a little tip I have found useful for things like this...

    Don't type the strings directly in the JavaScript file. Type them out as separate unquoted strings, one per line, and then save them in a file somewhere safe. Then, use a regular expression (under Advanced Find/Replace in Unitron) to generate the JS code from your text file. For example, if you start with

    abc
    def
    195

    ...etc, you can then use

    ^([a-z0-9]+)

    as your search string and

    "\1",

    as your replace string to change the strings into the right format for the JS code. If you then decide that you prefer the Array.Push(x) approach, you can just modify your regex replace string to

    arr.Push("\1");\n

    ...or whatever and do the find/replace again. A spreadsheet is a handy tool for things like this, too, because it is easy to number the lines and generate the arr[0] = "xxx" form.

    This technique saves a bit of typing and is "safe" because you can always generate the code again if you decide that another format would be clearer or easier to maintain, comment, etc.
     
  4. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Simple enough :).

    @Andeeee

    Thanks for that tip,

    Ray