|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
Pound signs in variable names
Hi folks,
This is a kinda wierd one. I've got some software here running on my CFMX server to handle e-commerce. I'm trying to create a custom front-end for some of the product categories but I'm running into a bit of trouble with how this software tracks users. Apparently the software sets a cookie when the user first visits and this cookie contains an ID that I need to use. Now it should be a simple matter to retrieve this information from the cookie except there is a catch... the variable in the cookie that CF uses to access this informaton is named: store1#CustomerID Yes, that's right. The variable name ITSELF contains a pound sign. This is causing a huge headache as the CF engine think's I'm calling a variable in this variables name when I try to do anything with it. For Example, I can try to access the data in the cookie var by using something like this: <cfset CookieData = cookie.store1#CustomerID> OR <cfset CookieData = cookie.store1##CustomerID> Both of these methods result in an "Invalid CFML Construct" error. Anyone have a suggestion? |
|
#2
|
|||
|
|||
|
You cannot have a pound sign in a variable name:
- A variable name must begin with a letter, underscore, or Unicode currency symbol. - The initial character can by followed by any number of letters, numbers, underscore characters, and Unicode currency symbols. - A variable name cannot contain spaces. I would try stripping the pound sign out of the cookie name before doing anything with it in CF. Try replace() or replaceNoCase(). It might be tricky.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums Last edited by kiteless : November 19th, 2004 at 04:39 PM. |
|
#3
|
|||
|
|||
|
That may be the case but that doesn't mean it's not possible to have a var name with a "#" character. I don't have control of how the var is set in the cookie, the code that does this is setup in a java class that I don't have source to.
I noticed that if I enable debugging I can see that the var is listed under "Cookie Variables" with the "#" character in the var name. Somehow CFMX can access it, how the heck can I? |
|
#4
|
||||
|
||||
|
It knows its there, of course, because it's in the Cookies collection. The problem is that a hash mark is a reserved character in CFML, and if it appears anywhere in the document then CF interprets that as the start of an insert.
One possible workaround is to use StructKeyArray. This will turn your cookies collection into an array and then you can try to figure out, through loops and such, where and what the value of these cookies are. You can also try to use Eval in CFScript for something like: Eval("newvar = Cookies.#cookiename") Really though, I'd suggest changing languages. Just do it in PHP or something else that doesn't have # as a reserved character since these cookies are so integral to your application. Hope that helps. |
|
#5
|
|||
|
|||
|
Like I said, replace the pound sign with somethine else. It can be done it just takes some fiddling. This replaces the pound sign with a tilde as an example.
<cfset theCookieName = "store1##customerID" /> <cfset cookieStruct = structNew() /> <cfset structInsert( cookieStruct, theCookieName, 'The Cookie Value' ) /> <cfloop collection="#cookieStruct#" item="thisKey"> <cfif findNoCase( '##', thisKey )> <cfset tempStruct = structNew() /> <cfset newKeyName = replaceNoCase( thisKey, '##', '~', 'All' ) /> <cfset structInsert( tempStruct, newKeyName, cookieStruct[thisKey] ) /> </cfif> </cfloop> <cfset cookieStruct = duplicate( tempStruct ) /> <cfdump var="#cookieStruct#"> Since pound signs are reserved characters in CF it won't let you directly use the variable if it has a pound sign in the name. Your only two choices are to replace the pound sign or not use ColdFusion. |
|
#6
|
|||
|
|||
|
Using a "#' sign
you can use
ampersand poundsign 35 semicolon no spaces between the characters this is the 'number sign' in html markup language go here to see it: http://www.w3.org/MarkUp/html-spec/html-spec_13.html#SEC13 |
|
#7
|
|||
|
|||
|
Read the cookie with Javascript, and then re-save it without the # sign?
|
|
#8
|
|||
|
|||
|
Just double up the hashes. That's the standard method for escaping them in ColdFusion. Be forwarned that you'll have to treet this variable name differently though. You'll never be able to use it in regular dot notation. You're stuck to using square brackets. So, instead of "cookie.store1#CustomerID", try "cookie['store1##CustomerID']". It's just that easy.
Jeff Howden |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Pound signs in variable names |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|