4.2. Testing Arrays

If you want to test whether a variable of type array is defined or undefined you can do the following:

Example 4-3. Testing a variable of type array to see if it is defined


  string *m;

  if (!undefinedp(m)) {
    write("is defined\n");
  } else {
    write("is not defined\n");
  }

  if (m) {
    write("is defined\n");
  } else {
    write("is not defined\n");
  }

This prints "is not defined" twice.

If you want to test whether a variable of type array is empty or not empty, you should use the sizeof function like this:

Example 4-4. Testing a variable of type array to see if it is defined


  string *m = ({ });

  if (sizeof(m) == 0) {
    write("empty\n");
  } else {
    write("non-empty\n");
  }

This will print "empty".

The following will NOT work for testing variables of type array:


  string *m = ({ });

  if (m == ({ }) {
    ...
  }

This will always be false. You may think you're testing to see if m is an empty array, but what you're actually doing is testing to see if one array is equal to another array. LPC cannot test whether one array is equal to another array regardless of the contents of each array. In order to do this correctly, you'd need a function.

The following will NOT work for testing variables of type array:


  string *m = ({ });

  if (m) {
    ...
  }

This will always be true because m is defined and has an array as a value. Regardless of whether the array is empty or non-empty, this test will always be true.