views:

59

answers:

1

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 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

+1  A: 

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.

Ben Doom
Ah! I had a feeling it might be nothing to do with the switch() statement. You're right though, I'm trying to modify the query string. DUH! So, is CGI.QUERY_STRING a simple variable?
Scott
Yes, it is a string.
Tyler Clendenin
Ben, you were right, it did need some tweaking (there's a missing parenthesis on the end of the if statement), but it works. Now, I'm unable to find a method to rebuild the URL; then again I've been staring at this thing all afternoon and am REALLY tired.
Scott
I fixed the missing paren -- thanks. You'll want to loop over the structure (in this case, arguments.scope). Scopes are structs, so look into the struct*() functions, like structKeyArray() and structCount().
Ben Doom
Yup, that ought to do it. Thanks. By the way, couldn't I just use StructUpdate() instead of the whole "if (structkeyexists" statement?
Scott
I hadn't ever used that one before (and didn't know about it). It looks like it will work fine, though.
Ben Doom