CFScript switch statement throws error when passed a string?
Update:
Thanks Ben, I decided to copy the URL to another structure and modify that one with StructUpdate(). Here's the code if anyone's interested (specific to my application, but you can edit the lines with comments to get a useful function).
function rebuildURL(key, value)
{
var URLstring = "";
var VarCount = 0;
var tmpURL = duplicate(URL);
var VarSeparator = "";
structUpdate(tmpURL, arguments.key, arguments.value);
for (key in tmpURL)
{
if (tmpURL[key] neq "" and tmpURL[key] neq "10000" and tmpURL[key] neq "1") `<!--- remove the tmpURL[key] neq "10000" and "1"--->`
{
if (VarCount neq 0)
{
VarSeparator = "&";
}
else
{
VarSeparator = "";
}
URLstring = URLstring & VarSeparator & "#Lcase(key)#" & "=#Lcase(tmpURL[key])#";
VarCount = VarCount + 1;
}
}
structClear(tmpURL); `<!---not sure if this is necessary, but probably can't hurt unless you are processing thousands of links --->`
return(URLstring);
}
Thanks again! Scott
Hey guys,
I'm writing a custom function to rework the URL for links in my pages, and I'm getting the following error:
Complex object types cannot be converted to simple values.
The expression has requested 开发者_StackOverflow社区a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a cfif tag.
The error occurred in C:\ColdFusion8\wwwroot\pascalnew\turbos.cfm: line 8 Called from C:\ColdFusion8\wwwroot\pascalnew\turbos.cfm: line 108 Called from C:\ColdFusion8\wwwroot\pascalnew\turbos.cfm: line 93 Called from C:\ColdFusion8\wwwroot\pascalnew\turbos.cfm: line 1 Called from C:\ColdFusion8\wwwroot\pascalnew\turbos.cfm: line 1
6 : URLvar = "#URL#";
7 : switch(param)
8 : {
9 : case 'mfr':
10 : {
Here's my function code:
<cfscript>
function SetURL(param, paramval)
{
URLvar = "#URL#";
switch(param)
{
case 'mfr':
{
IF (URLvar contains "mfr")
{
REReplaceNoCase(URLvar, "mfr=^[^\&]", "mfr=#paramval#", "All");
}
break;
}
}
return(URLvar);
}
</cfscript>
Here's what I was testing it with:
<cfset urlvar = SetUrl("mfr", "edwards")>
<cfdump var="#urlvar#">
How is "mfr" a complex variable??
Thanks, Scott
When you use CFScript, some versions report the beginning of the block as the line with the error.
When you assign "#URL#" to URLVar, you are creating a pointer to the URL scope. then, you attempt to use the contains operator on it. Contains, however, only compares two simple values.
So, your attempt to reference a complex value as a scalar actually comes here:
IF (URLvar contains "mfr")
{
REReplaceNoCase(URLvar, "mfr=^[^\&]", "mfr=#paramval#", "All");
}
At a guess, you are trying to look at the URL itself, not the URL scope. You can assemble this from parts of the CGI scope, including SERVER_NAME, SCRIPT_NAME, AND QUERY_STRING (or you can look at the individual part you need).
Added: If you want to know if a variable is passed in the url, I think you are overthinking this. Let's say you have a param and a paramval to replace it with. You could do it like this:
function paramReplace(param, paramVal, scope)
{
if(structkeyexists(arguments.scope, arguments.param))
{
arguments.scope[arguments.param] = arguments.paramVal;
}
}
paramReplace("mfr", "fred", URL);
This simply uses structKeyExists to find out if that variable exists in the appropriate scope, then replaces the value if it does. If you need to rebuild your actual query string, you can do so later. This avoids scenarios where you get bad data if your query string contains something like "zone=mfr".
I've not tested this -- it's off the cuff -- so it may need tweaking, but it should get you started.
精彩评论