StructEach

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] [, maxThreads])

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.

maxThreads

(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<=10000;i++){ 
mystruct.insert("key#i#","val#i#") 
 
} 
function callback(key,val){ 
writeoutput(key & " " & val & "<br>") 
 } 
   
 t_start=GetTickCount() 
 mystruct.each(callback) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with no parallel:" &  t_end-t_start) 
 
 t_start=GetTickCount() 
 mystruct.each(callback,true,5); 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 5 threads:" &  t_end-t_start) 
  
  
 t_start=GetTickCount() 
 mystruct.each(callback,true,10) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 10 threads:" &  t_end-t_start) 
  
 t_start=GetTickCount() 
 structeach(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 20 threads:" &  t_end-t_start) 
  
 t_start=GetTickCount() 
 structeach(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40) 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 40 threads:" &  t_end-t_start) 
  
 
</cfscript>

 Adobe

Get help faster and easier

New user?

Adobe MAX 2024

Adobe MAX
The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX 2024

Adobe MAX
The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online