StructEach

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

This function loops over elements in a structure by accessing key-value pairs.

Returns

Void

Category

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

Syntax

structEach(struct,function(key, value [, struct]){} [, parallel] [, maxThreadCount])

Parameters

Parameter

Description

struct

Name of the structure object.

function

Inline function executed for each key - value pair in the struct.

key

Key in a struct.

value

Value in a 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 using an inline function

<cfscript>
       myStruct=StructNew();
       myStruct = {a:1,b=2,c=3,d=4,e=5}; // Define key-value pairs
       // Run StructEach with an inline function
       StructEach(myStruct,function(key,value) {
       WriteOutput('The value of ' & key & ' is ' & value & '; ');
});
</cfscript>

Output

The value of a is 1; The value of b is 2; The value of c is 3; The value of d is 4; The value of e is 5;

Example using a named function

<cfscript>
       myStruct=StructNew();
       myStruct = {team:"Chicago Bulls",player:"Michael Jordan", sport:"basketball"}; // Define key-value pairs
       // Create a named function that takes two arguments as key and value
      function getValues(key, value) { 
      writeOutput('My favorite ' & key & ' is ' & value & " | "); 
      } 
      // Run StructEach with the named function getValues
      structEach(myStruct,getValues); 
</cfscript>

Output

My favorite player is Michael Jordan | My favorite sport is basketball | My favorite team is Chicago Bulls |

Using member function

<cfscript>
       myStruct = {a:1,b=2,c=3,d=4,e=5}; // Define key-value pairs
       myStruct.each(function(key,value){
             WriteOutput("#key#:#value#");
       });
</cfscript>

Using parallelization

<cfscript> 
mystruct= Structnew("ordered"); 
for(i=1;i<=100;i++){ 
mystruct.insert("key#i#","val#i#") 
 
} 
function callback(key,val){ 
writeoutput(key & " " & val & "<br>") 
 } 
   
 
 mystruct.each(callback,false) 
  
 mystruct.each(callback,true); 
  
 mystruct.each(callback,true,10) 
  
 structeach(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20) 
  
 structeach(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40) 
  
 try{structeach(struct=mystruct,callback=callback,parallel=true,maxthreadcount=-20)} 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
} 
 try{ 
  
 structeach(struct=mystruct,callback=callback,parallel=true,maxthreadcount="200")} 
 catch(any e){ 
  writeoutput("<br>Type: " & e.type & " Message:" &  e.message ) 
} 
  
 
</cfscript>
Adobe logo

Sign in to your account