0
for(var i:int=0;i<stringArray.length;i++)
{
    if(stringArray[i]==""||stringArray[i]==null)
    {
        trace("Element at "+i+" found empty");
    }
}

I have a string array in AS3. Now I want to check if a particular element at index i is not set, how can i do this?

Error I get with the above code when a string that is not set arrives is as follows -

RangeError: Error #1125: The index 2 is out of range 2.
5
  • set some default value which has the meaning "not set" and check for equality with that value Commented Feb 27, 2013 at 19:19
  • Is that really the code that gave the RangeError? Seems odd that it could. Commented Feb 27, 2013 at 20:12
  • @LarsBlåsjö Not exactly, the one I was using was a Vector.<Array> with each array containing strings, a bit difficult to understand, so I gave a simpler code sample. Commented Feb 27, 2013 at 20:45
  • OK, I understand your thought about simplifying the example, but in this case it was probably not such a good idea, and saying "error I get with the above code", since the code can't give that kind of error. So unless your actual code is super complex, it is probably better if you edit your question to include it, or I don't see how you can get a correct answer. Commented Feb 27, 2013 at 20:52
  • @LarsBlåsjö you were right. error was due to away3d library, not with the code I provided. Thanks for pointing Commented Feb 27, 2013 at 20:56

4 Answers 4

5

Since an empty string and null and undefined are all falsy, regarded as boolean false in a conditional statement, and you say your array will only contain strings, you should be able to check this way:

for(var i:int=0;i<stringArray.length;i++)
{
    if(!stringArray[i])
    {
        trace("Element at "+i+" found empty");
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

1

Check for equality with undefined to see if a certain index has not been set.

5 Comments

should i check with undefined or "undefined"
if (stringArray[i] == undefined) ought to do it.
As of ActionScript 3 a 'String' default value is 'null' not 'undefined' -> if(!stringArray[i]) { ... }
Indeed, but it's not a string yet - it's undefined. Adobe says; "Arrays are sparse arrays, meaning there might be an element at index 0 and another at index 5, but nothing in the index positions between those two elements. In such a case, the elements in positions 1 through 4 are undefined, which indicates the absence of an element, not necessarily the presence of an element with the value undefined."
Though !stringArray[i] will work also. But that would return a false negative if the boolean value false was stored in the array.
0

Maybe it's just a matter of adding a bounding condition to check if the passed array index is within the range, something like:

function isValidStringInArray(index:int,array:Array):Boolean{
   if(index >= 0 && index < array.length) return ((array[index] != null || array[index] != undefined || array[index].length > 0);
   else return false;
}

2 Comments

The item could be within bounds and still not set
@Chadyk true, which is why, after making sure it's within bounds(to avoid the RangeError, which seems like the main problem) the function checks for null/undefined/empty strings
0

As it turns out, the error had nothing to do with the code I provided, it was an error with bitmap materials in away3d library, which is completely different from the question I asked. But good answers anyway.

Thanks to Lars comment, I thought about other possibilities of errors too, and found it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.