Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Checking arrays

Discussion in 'Scripting' started by reset, Mar 2, 2010.

  1. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Hi

    In the code below I am getting the error "Argument Out of Range" - ie if x = 0 and y = 0 then the array element c[-1][0] does not exist.

    How do I check this properly?

    thanks


    Code (csharp):
    1. if(c[x-1][y]) {
    2.             ret.Push(c[x-1][y]);
    3.         }
     
  2. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    What exactialy are you trying to do? Nested arrays?
     
  3. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Yeah, I have a 2D array.

    I know that c[-1][0] does not exist but I assumed from my script that if it does not exist (False) then it skips the the array push - if it does exist then the element is pushed into the array (ret).
     
  4. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    What would happen if you used the range 0 < x < infinity? I dont think it is possible to use an array value of -1 at all. Perhaps shifting the array so that the beginning value is 1?
     
  5. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    You need to figure out for what cases you are trying to reach the negative. That is, decide what you want to do when for instance x == 0. Then simply add checks for going out of bounds. Depending on how you want to handle your special cases the if statements will have to be nested, or just one big check to make sure conditions are "perfect". So, either:

    Code (csharp):
    1. if( x < 1 ) {
    2. //handle this case
    3. } else if ( y < 0 ) {
    4. //handle this case
    5. } else if ( x >= c.Length ) {
    6. //handle this case
    7. } else if ( y >= c[x-1].Length ) {
    8. //handle this case
    9. } else {
    10. ret.Push(c[x-1][y]);
    11. }
    or else just

    Code (csharp):
    1.  
    2. if ( x > 1  y > 0  x <  x < c.Length  y < c[x-1].Length ) {
    3. ret.Push( c[x-1][y] );
    4. }
     
  6. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    I cant understand why I there is not an ability to check for an array's element existence - that if it is true then return the array

    For example: if x = 0 and y = 0 then the first line would find that the element in c array of c[-1][0] did not exist and would therefore would skip the if-statement

    Code (csharp):
    1. c = new Array();
    2.  
    3. for (x=0; x<8; x++) {
    4.    c[x] = new Array();
    5.     for(y=0;y<8;y++){
    6.     c[x][y] = 1;
    7.         }
    8. }
    9.  
    10. ret = new Array();
    11.  
    12. var x = 0;
    13. var y = 0;
    14.  
    15. if(c[x-1][y]) {ret.Push(c[x-1][y]);}
    16. if(c[x+1][y]) {ret.Push(c[x+1][y]);}
    17. if(c[x][y+1]) {ret.Push(c[x][y+1]);}
    18. if(c[x][y-1]) {ret.Push(c[x][y-1]);}
    19.  
     
  7. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Plain and simple, you cannot request an array element at index -1 or at a position greater than or equal to the length. Those are invalid indices and will cause errors.
     
  8. zelk

    zelk

    Joined:
    Mar 13, 2009
    Posts:
    246
    You can use a try-catch-block but if you use it a lot, it may affect performance:

    Code (csharp):
    1.  
    2. try
    3. {
    4.     ret.Push(c[x-1][y]);
    5. }
    6. catch(Exception){}
    7.  
    Not very beautiful code but it should work. A little like the old Basic "ON ERROR RESUME NEXT"...

    Good luck!

    /Ricky
     
  9. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    Well, there is. You can check for the existence of an element in any part of the array. The way to check for existence of an element in an array and doing it by looking outside the array probably shouldn't exist.

    I can see where that might seem handy for some specific ways of handling arrays, which is exactly why it's something you, as a programmer, need to check for specifically. In general it'd be more of a nuisance to program in an environment that makes assumptions about what you were trying to do and not to, or to have to deal with built-in arrays that are slower and more cumbersome due to added "features" like that.

    Sensibly handling exceptions to the normal cases is a big part of writing sensible programs.
     
  10. pedr0

    pedr0

    Joined:
    Feb 28, 2010
    Posts:
    29
    Remember that -1 index are ok index for Javascript builtin array: it represent the last element of array.
    var _test = [1,2,3,4,5,6];
    print(_test[-1]);

    It will print :
    6

    If your code raise an exception is only because the index is greater the length of array.

    Important : all it's true only for builtin array!