User Guide Cancel

StructFind

 

Note:

You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions.

Description

Determines the value associated with a key in a structure.

Returns

The value associated with a key in a structure; if a structure does not exist, the function throws an exception.

Category

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release):

  • Takes a callback instead of the key value as a parameter.

Syntax

structFind(structure, closure, [, parallel][, maxThreadCount] )
structFind(structure, closure, [, parallel][, maxThreadCount] )
structFind(structure, closure, [, parallel][, maxThreadCount] )

See also

Structure functionsStructure functions in the Developing ColdFusion Applications

Parameters

Parameter

Description

structure

Structure that contains the value to return.

closure

Takes a callback function. You can use this instead of the key value.

key

Key whose value to return. You can also use this instead of the callback.

parallel

True if you want to enable parallel programming.

Note: Parallelism is supported only when the second parameter is a closure. When the second parameter is a key, paralellism is not supported.

maxThreadCount

The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception.

Example

<cfscript>
myStruct=StructNew();
myStruct={a:1,b:2,c:3,d:4,e:5}; //Define structure keys
WriteOutput("The values are: ");
for (key in myStruct){ //Find keys in the struct
WriteOutput(StructFind(myStruct,#key#) & " | "); //Display the value if key is found
}
// Try to search for the key "f" that does not exist in myStruct
// Since the key is not in the struct, an error message is displayed
try{
StructFind(myStruct,"f");
}
catch (any e)
{
WriteOutput(e.message);
}
</cfscript>
<cfscript> myStruct=StructNew(); myStruct={a:1,b:2,c:3,d:4,e:5}; //Define structure keys WriteOutput("The values are: "); for (key in myStruct){ //Find keys in the struct WriteOutput(StructFind(myStruct,#key#) & " | "); //Display the value if key is found } // Try to search for the key "f" that does not exist in myStruct // Since the key is not in the struct, an error message is displayed try{ StructFind(myStruct,"f"); } catch (any e) { WriteOutput(e.message); } </cfscript>
<cfscript>
       myStruct=StructNew();
       myStruct={a:1,b:2,c:3,d:4,e:5}; //Define structure keys
       WriteOutput("The values are: ");
       for (key in myStruct){ //Find keys in the struct
             WriteOutput(StructFind(myStruct,#key#) & " | "); //Display the value if key is found
       }
       // Try to search for the key "f" that does not exist in myStruct
       // Since the key is not in the struct, an error message is displayed
       try{
             StructFind(myStruct,"f");
       }
       catch (any e)
       {
             WriteOutput(e.message);
       }
</cfscript>

Output

The values are: 1 | 2 | 3 | 4 | 5 | 

Cannot find f key in structure.

Using member function

<cfscript>
myStruct={a:1,b:2,c:3,d:4,e:5}; //Define structure keys
myFind=myStruct.find("c");
WriteOutput(myFind); // Displays value of key c that is 3
</cfscript>
<cfscript> myStruct={a:1,b:2,c:3,d:4,e:5}; //Define structure keys myFind=myStruct.find("c"); WriteOutput(myFind); // Displays value of key c that is 3 </cfscript>
<cfscript>
       myStruct={a:1,b:2,c:3,d:4,e:5}; //Define structure keys
       myFind=myStruct.find("c"); 
       WriteOutput(myFind); // Displays value of key c that is 3
</cfscript>

Using parallelization

<cfscript>
mystruct= Structnew("ordered");
for(i=1;i<=10000;i++){
mystruct.insert("key#i#","val#i#")
}
function callback(key,val){
if(isSimplevalue(val) && key eq 'key889')
return true
return false
}
writeoutput("<br> mystruct.find(callback,false)" & mystruct.find(callback))
writeoutput("<br> mystruct.find(callback,true):" & mystruct.find(callback,true,5));
writeoutput( "<br> mystruct.find(callback,true,10):" & mystruct.find(callback,true,10))
writeoutput( "<br> mystruct.find(callback,true,20):" & structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20))
writeoutput( "<br> mystruct.find(callback,true,40):" & structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40))
try{
structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-40)
}
catch(any e){
writeoutput("<br>Type: " & e.type & " Message:" & e.message )
}
try{
structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=400)
}
catch(any e){
writeoutput("<br>Type: " & e.type & " Message:" & e.message )
}
</cfscript>
<cfscript> mystruct= Structnew("ordered"); for(i=1;i<=10000;i++){ mystruct.insert("key#i#","val#i#") } function callback(key,val){ if(isSimplevalue(val) && key eq 'key889') return true return false } writeoutput("<br> mystruct.find(callback,false)" & mystruct.find(callback)) writeoutput("<br> mystruct.find(callback,true):" & mystruct.find(callback,true,5)); writeoutput( "<br> mystruct.find(callback,true,10):" & mystruct.find(callback,true,10)) writeoutput( "<br> mystruct.find(callback,true,20):" & structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)) writeoutput( "<br> mystruct.find(callback,true,40):" & structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)) try{ structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-40) } catch(any e){ writeoutput("<br>Type: " & e.type & " Message:" & e.message ) } try{ structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=400) } catch(any e){ writeoutput("<br>Type: " & e.type & " Message:" & e.message ) } </cfscript>
<cfscript> 
mystruct= Structnew("ordered"); 
for(i=1;i<=10000;i++){ 
mystruct.insert("key#i#","val#i#") 
 
} 
function callback(key,val){ 
if(isSimplevalue(val) && key eq 'key889') 
 return true 
return false 
 } 
   
  
 writeoutput("<br> mystruct.find(callback,false)" & mystruct.find(callback)) 
  
 writeoutput("<br> mystruct.find(callback,true):" & mystruct.find(callback,true,5)); 
  
 writeoutput( "<br> mystruct.find(callback,true,10):" & mystruct.find(callback,true,10)) 
  
 writeoutput( "<br> mystruct.find(callback,true,20):" & structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)) 
  
 writeoutput( "<br> mystruct.find(callback,true,40):" & structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)) 
 try{ 
 structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-40) 
 } 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
} 
 try{ 
 structfind(struct=mystruct,callback=callback,parallel=true,maxthreadcount=400) 
 } 
catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
}  
 
</cfscript>

Get help faster and easier

New user?