×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Arrays

Check for existing array element by TruthInSatire
Posted: 18 Jan 05

The built in function isDefined() can not test arrays.  Attempting a test will produce an error.

isdefined("myArray[1]")
produces

Quote (error):


Parameter 1 of function IsDefined, which is now "myArray[1]", must be a syntactically valid variable name.

In order to test for the existance of an array element this UDF should do the trick.

USE: isArrayElementDefined("myArray[element]")
(This function also works for multi-dimensional arrays)

CODE

<cfscript>
    //define the function
    function isArrayElementDefined(arrayName){
          //use try{} to attempt using the array element
          try{
            //use the array element and return true
            testVar = evaluate(arrayName);
            return true;

          //if using it causes an error catch it and return false
          }catch(any test){
            return false;
        }
    }
</cfscript>

EXAMPLE:

CODE

<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "bob">
<cfset myArray[3] = "joe">
<cfoutput>
<cfloop from = "1" to = "3" index = "place">
#isArrayElementDefined("myArray[#place#]")#
</cfloop>
</cfoutput>
OUTPUT: true false true

Back to Adobe FAQ Index
Back to Adobe Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close