Checking arrays

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

``````if(c[x-1][y]) {
ret.Push(c[x-1][y]);
}
``````

What exactialy are you trying to do? Nested arrays?

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).

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?

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:

``````if( x < 1 ) {
//handle this case
} else if ( y < 0 ) {
//handle this case
} else if ( x >= c.Length ) {
//handle this case
} else if ( y >= c[x-1].Length ) {
//handle this case
} else {
ret.Push(c[x-1][y]);
}
``````

or else just

``````if ( x > 1  y > 0  x <  x < c.Length  y < c[x-1].Length ) {
ret.Push( c[x-1][y] );
}
``````

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

``````c = new Array();

for (x=0; x<8; x++) {
c[x] = new Array();
for(y=0;y<8;y++){
c[x][y] = 1;
}
}

ret = new Array();

var x = 0;
var y = 0;

if(c[x-1][y]) {ret.Push(c[x-1][y]);}
if(c[x+1][y]) {ret.Push(c[x+1][y]);}
if(c[x][y+1]) {ret.Push(c[x][y+1]);}
if(c[x][y-1]) {ret.Push(c[x][y-1]);}
``````

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.

You can use a try-catch-block but if you use it a lot, it may affect performance:

``````try
{
ret.Push(c[x-1][y]);
}
catch(Exception){}
``````

Not very beautiful code but it should work. A little like the old Basic “ON ERROR RESUME NEXT”…

Good luck!

/Ricky

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.

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!