User Guide Cancel

StructFilter

 

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

Filters the key-value pairs in a struct.

Returns

Filtered struct.

Category

Syntax

structFilter(struct,callback [, parallel] [, maxThreadCount])
structFilter(struct,callback [, parallel] [, maxThreadCount])
structFilter(struct,callback [, parallel] [, maxThreadCount])

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release): Introduced named parameters.

ColdFusion 10: Added this function.

Parameters

Parameter

Description

struct

Name of the struct object.

callback

Inline function executed for each element in the array. Returns true if the key value pair in the struct has to be included in the resultant struct.

parallel

(Boolean)- True if you want to enable parallel programming.

maxThreadCount

(Int) 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={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016};
WriteOutput("The input, unfiltered struct is:");
WriteDump(myStruct);
myFilteredStruct=StructFilter(myStruct,function(key,value){
return value>8; //Return only those values >8
}
);
WriteOutput("The filtered struct is:");
WriteDump(myFilteredStruct);//Display the filtered struct
</cfscript>
<cfscript> myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016}; WriteOutput("The input, unfiltered struct is:"); WriteDump(myStruct); myFilteredStruct=StructFilter(myStruct,function(key,value){ return value>8; //Return only those values >8 } ); WriteOutput("The filtered struct is:"); WriteDump(myFilteredStruct);//Display the filtered struct </cfscript>
<cfscript>
       myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016};
       WriteOutput("The input, unfiltered struct is:");
       WriteDump(myStruct);
       myFilteredStruct=StructFilter(myStruct,function(key,value){
             return value>8; //Return only those values >8
       }
       );
       WriteOutput("The filtered struct is:");
       WriteDump(myFilteredStruct);//Display the filtered struct
</cfscript>

Output

Using member function

<cfscript>
myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016};
CFVersions=myStruct.filter(function(key,value){
return value>8;
});
WriteDump(CFVersions);
</cfscript>
<cfscript> myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016}; CFVersions=myStruct.filter(function(key,value){ return value>8; }); WriteDump(CFVersions); </cfscript>
<cfscript>
       myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016};
       CFVersions=myStruct.filter(function(key,value){
             return value>8;
       });
       WriteDump(CFVersions);
</cfscript>

Using parallelization

<cfscript>
mystruct= Structnew("ordered");
for(i=1;i<=10000;i++){
mystruct.insert("key#i#","val#i#")
}
function callback(key,val){
if(isObject(val))
eturn false
else if(isSimplevalue(val) && key eq 'key889')
{
return true
}
else return false
}
writeoutput(mystruct.filter(callback,false)[
"key889"])
writeoutput(mystruct.filter(callback,true)[
"key889"]);
writeoutput(mystruct.filter(callback,true,10)[
"key889"])
writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)[
"key889"])
writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)[
"key889"])
try{
writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-20)[
"key889"])
}
catch(any e){
writeoutput("<br>Type: " & e.type & " Message:" & e.message )
}
try{
mystruct.filter(callback,true,100)}
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(isObject(val)) eturn false else if(isSimplevalue(val) && key eq 'key889') { return true } else return false } writeoutput(mystruct.filter(callback,false)[ "key889"]) writeoutput(mystruct.filter(callback,true)[ "key889"]); writeoutput(mystruct.filter(callback,true,10)[ "key889"]) writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)[ "key889"]) writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)[ "key889"]) try{ writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-20)[ "key889"]) } catch(any e){ writeoutput("<br>Type: " & e.type & " Message:" & e.message ) } try{ mystruct.filter(callback,true,100)} 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(isObject(val)) 
   eturn false 
  else if(isSimplevalue(val) && key eq 'key889') 
  {  
   return true 
  } 
  else return false 
 } 
   
  
 writeoutput(mystruct.filter(callback,false)[ 
 "key889"]) 
  
 writeoutput(mystruct.filter(callback,true)[ 
 "key889"]); 
  
 writeoutput(mystruct.filter(callback,true,10)[ 
 "key889"]) 
  
 writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)[ 
 "key889"]) 
  
 writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)[ 
 "key889"]) 
 try{ 
 writeoutput(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-20)[ 
 "key889"]) 
 } 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
 } 
 try{ 
 mystruct.filter(callback,true,100)} 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
 } 
</cfscript>

Get help faster and easier

New user?