Solved

Displaying Session variables with multiple levels of indirection

Posted on 2007-11-15
6
135 Views
Last Modified: 2013-12-24
I am trying to provide macro-like substitutions for strings from an input file. The input file might have the string "The user is: !user!."

Suppose Session.user = "Steve"

I want to output the string with !user! replaced with the value of "Session.user".

If I use:
       <cfset line = REreplaceNoCase(#line#, '!(.*)!', '#Session.user#')>
       <cfset line = "#line#">

I get "The user is: Steve." That is, this works, but "!xxxxxx!" would be replace with the value of Session.user.

If I use:
       <cfset replaceString = "Session.\1">
       <cfset line = REreplaceNoCase(#line#, '!(.*)!', '#replaceString#')>
       <cfset line = "#line#">
                      <cfoutput>#line#</cfoutput>

I get "The user is: Session.user."

If I use:
       <cfset replaceString = "Session.\1">
       <cfset testReplace = "#replaceString#">
       <cfset line = REreplaceNoCase(#line#, '!(.*)!', '#testReplace#')>
       <cfset line = "#line#">
                      <cfoutput>#line#</cfoutput>

I get "The user is: Session.user."

If I use:
       <cfset replaceString = "Session.\1">
       <cfset testReplace = "###replaceString###">
       <cfset line = REreplaceNoCase(#line#, '!(.*)!', '#testReplace#')>
       <cfset line = "#line#">
                      <cfoutput>#line#</cfoutput>

I get "The user is: #Session.user#."

I need to have two evaluations. Thanks much for any help.
0
Comment
Question by:stevenschulman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 20293013
Perhaps I'm missing something but is there a reason a straight replace wouldn't work?


<cfset theNewLine = replace(line, '!user!', Session.user, "all")>

Open in new window

0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 20298513
>>I want to output the string with !user! replaced with the value of "Session.user".

If I use:
       <cfset line = REreplaceNoCase(#line#, '!(.*)!', '#Session.user#')>
       <cfset line = "#line#">

I get "The user is: Steve." That is, this works, but "!xxxxxx!" would be replace with the value of Session.user.

isn't that what you want? or am i not reading the first line right?
what output are you looking to achieve?
0
 

Author Comment

by:stevenschulman
ID: 20300110
!user! is only an example. If the text were "Hello !firstName! !lastName!" then !firstName! would be substituted with the value of Session.firstName and !lastName! would be substituted with the value of Session.lastName.
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 25

Expert Comment

by:James Rodgers
ID: 20300235
then you should not use a regex but a specific replace
<cfset temp=#replace(line,"!somestring!", #session.somestring#,'all')>
reg ex is for pattern matching you need exact matching

the pattern you are using, !(.*)!',  will match !user!  and !xxxxxx! but it would also match
this sentence ending an an exclamation! and the next sentence ending in an exclamation!
would look like this
this sentence ending an an exclamationSteve
so use exact match replacement
0
 

Author Comment

by:stevenschulman
ID: 20300332
I understand that, but with regular expressions whatever is enclosed in parenthesis is returned and reusable as \n. Thus, in this case, "!(.*)!" matches all characters between two exclamaition points and provides the specific values matched in "\1|. That is why I use "Session.\1". This does, in fact, work. I get the value "Session.user" or "Session.firstName" or "Session.lastName". The problem is that I have the string, but can not seem to go the next step to have it evaluated.

I tried using Evaluate() function. I suspect that the answer lies there somewhere, but I still can not get it to work.
0
 
LVL 25

Accepted Solution

by:
James Rodgers earned 500 total points
ID: 20300405
of then set the replace as #session.\1*

and tehn before pout[utting try this

#de(line)#
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question