structblend
Example Output
St1 Orginal - struct
BASE Struct One Keyval
NEST
St1 Orginal - struct
ONE one
THREE three
TWO two
St2 - struct
BASE Struct 2 Keyval
NEST
St2 - struct
FOUR 4
TWO 2
Blend with overwrite=true: Return=true
St1 blended - struct
BASE Struct One Keyval
NEST
St1 blended - struct
FOUR 4
ONE one
THREE three
TWO two
Blend with overwrite=false: Return=true
St1 blended, overwrite=false - struct
BASE Struct 2 Keyval
NEST
St1 blended, overwrite=false - struct
FOUR 4
ONE one
THREE three
TWO 2
        
Source code <cfscript> St1.Base = "Struct One Keyval"; St1.Nest.one = "one"; St1.Nest.two = "two"; St1.Nest.three = "three"; St2.Base = "Struct 2 Keyval"; St2.Nest.two = 2; St2.Nest.four = 4; // Application.udf.structBlend(v.one,v.two,'yes'); </cfscript> <cfoutput> <cfdump var="#St1#" label="St1 Orginal"> <cfdump var="#St2#" label="St2"> Blend with overwrite=true: Return=<cfoutput>#structBlend(St1,St2,false)#</cfoutput> <cfdump var="#St1#" label="St1 blended"> Blend with overwrite=false: Return=<cfoutput>#structBlend(St1,St2,true)#</cfoutput> <cfdump var="#St1#" label="St1 blended, overwrite=false"> </cfoutput> <cfscript> // Blends all nested structs, arrays, and variables in a struct to another. (structAppend for nested structs) // Returns true for sucessful, false otherwise. // @param Struct1 (Struct) (Required) - Structure to which struct2 is blended with. // @param Struct2 (Struct) (Required) - Structure that contains the data to blend with Struct1. // @param Delimitor (String) (Optional) [Default=true] - True/False: True = Does not overwrite strings/arrays already existing in Struct1. // Author Raymond Compton usaRaydar@gmail.com 2007.7.7 function structBlend(Struct1,Struct2) { var i = 1; var OverwriteFlag = true; var StructKeyAr = listToArray(structKeyList(Struct2)); var Success = true; if ( arrayLen(arguments) gt 2 AND listFindNoCase("0,no,false",Arguments[3]) ) // Optional 3rd argument "OverwriteFlag" OverwriteFlag = false; try { for ( i=1; i lte structCount(Struct2); i=i+1 ) { if ( not isDefined('Struct1.#StructKeyAr[i]#') ) // If structkey doesn't exist in Struct1 Struct1[StructKeyAr[i]] = Struct2[StructKeyAr[i]]; // Copy all as is. else if ( isStruct(struct2[StructKeyAr[i]]) ) // else if key is another struct Success = structBlend(Struct1[StructKeyAr[i]],Struct2[StructKeyAr[i]],OverwriteFlag); // Recall function else if ( OverwriteFlag ) // if Overwrite Struct1[StructKeyAr[i]] = Struct2[StructKeyAr[i]]; // set Struct1 Key with Struct2 value. } } catch(any excpt) { Success = false; } return Success; } </cfscript>
© 2017 usaRaydar
v.
IP:23.20.86.177 css:/assets/css/raycomweb.css?v=16021511