updkeyvals
Example Output
Variable examples - struct
VAR1 one=1&two=2&three=3&four=4
VAR2 one=1,two=2,three=3,four=4

Example 1: 1 param updating cgi.Query_String.
#updKeyVals(Variables.Ex.Var1)#
one=1&two=2&three=3&four=4
Example 2: 2 params updating existing list with "&" delims.
#updKeyVals("one=won&two=&three=triple&five=5",Variables.Ex.Var1)#
one=won&three=triple&four=4&five=5
Example 3: 3 params to update old list with new list with specified delim.
#updKeyVals("one=,four=fore,five=5",Variables.Ex.Var2,",")#
two=2,three=3,four=fore,five=5
        
Source code <cfset Variables.Ex.Var1 = updKeyVals("one=1&two=2&three=3&four=4")> <cfset Variables.Ex.Var2 = "one=1,two=2,three=3,four=4"> <cfdump var="#Variables.Ex#" label="Variable examples"> <div> <cfoutput> <hr /> <u>Example 1: 1 param updating cgi.Query_String.</u><br /> ##updKeyVals(Variables.Ex.Var1)##<br /> <strong>#updKeyVals(Variables.Ex.Var1)#</strong> <hr /> <u>Example 2: 2 params updating existing list with "&" delims.</u></br /> ##updKeyVals("one=won&two=&three=triple&five=5",Variables.Ex.Var1)##<br /> <strong>#updKeyVals("one=won&two=&three=triple&five=5",Variables.Ex.Var1)#</strong> <hr /> <u>Example 3: 3 params to update old list with new list with specified delim.</u> <br />##updKeyVals("one=,four=fore,five=5",Variables.Ex.Var2,",")## <br /><strong>#updKeyVals("one=,four=fore,five=5",Variables.Ex.Var2,",")#</strong> </cfoutput> </div> <cfscript> function updKeyVals(kvNew) { // Updates key values in a list such as cgi.Query_String. // @param kvNew (String/List) (Required) - List of keys=values to update. // @param kvList (String/List) (Optional) - Existing list of key=values. DEFAULT = cgi.Query_String. // @param Delim (Character) (Optional) - Delimiter to be used: DEFAULT="&". // @Return (String) - Returns a list with updated/added/deleted key=values. // Null values for key will delete key ("keyname=" will delete keyname) // Author Raymond Compton usaRaydar@gmail.com 2008.3.7 var Delim = "&"; // Default Delim var kvList = cgi.Query_String; // Default list of old KeyVals var kvAry = arrayNew(1); // Array of Old and New KeyVals var kvReturn = ''; // Contains newly assembled list of new KeyVals var kList = ''; // List containing processed KeyNames var kName = ''; var kPos = 0; var i = 0; if ( arrayLen(arguments) gt 2 ) Delim = Arguments[3]; // Optional 3rd Argument (List Delimiter) if ( arrayLen(arguments) gt 1 ) kvList = Arguments[2]; // Optional 2nd argument (Existing List of key=value) // Set list to array for simpler looping kvAry = listToArray(listAppend(kvList,kvNew,Delim),Delim); for(i = 1; i lte arrayLen(kvAry); i = i + 1) { // Loop thru list of all keyvals kName = listFirst(kvAry[i],"="); // set name of key kPos = listFindNoCase(kList,kName); // Find position of key in new keylist (if exists) if ( listRest(kvAry[i],"=") neq '' ) { // If key has value if ( kPos eq 0 ) { // if not duplicate key kvReturn = listAppend(kvReturn,kvAry[i],Delim); // append to kvReturn kList = listAppend(kList,kName); // add key to keylist } else // else duplicate key found kvReturn = listSetAt(kvReturn,kPos,kvAry[i],Delim); // Update kvReturn } else if ( kPos gt 0 ) { // Else is empty value kvReturn = listDeleteAt(kvReturn,kPos,Delim); // delete from kvReturn kList = listDeleteAt(kList,kPos); // delete from new keylist } } // return kvReturn; } </cfscript>
© 2017 usaRaydar
v.
IP:54.90.92.204 css:/assets/css/raycomweb.css?v=16021511