Pulling a transaction number from a string (coldfusion)

Posted on 2011-10-26
Medium Priority
Last Modified: 2012-05-12
I have a string that I need to pull a transaction number (follows TransactionID~) from.  Here is the string:
RESPONSE~101. Your transaction was successfully approved.|TRANSACTIONID~17514157|APPCODE~TAS331|APPMSG~ NO MATCH |AVSRESPONSE~No Match|CSCRESPONSE~Match|

It does not seem like this should be that hard, but it can't figure it out. I am not sure if the TransactionID is always an 8 digit number or if it will vary.

Thank you.
Question by:lonnyo
  • 2
  • 2
LVL 53

Accepted Solution

_agx_ earned 2000 total points
ID: 37035031
If you're certain "TRANSACTIONID" is always the 2nd element, just the string as nested lists delimited by "|" and "~"

   <cfset transactionID = getToken(getToken(yourString, 2, "|"), 2, "~")>

But a more robust approach is to parse the entire thing into a structure. Then you can reference any of the keys by name:   #structName.TransactionID#

<!--- split the results into "name~value" pairs --->
<cfset results = {}>
<cfset responseArr = listToArray(yourString, "|", true)>
<cfloop array="#responseArr#" index="elem">
      <!--- extract the name/value of each pair --->
      <cfset pair = listToArray(elem, "~", true)>
      <cfif arrayLen(pair) gte 2>
            <cfset results[trim(pair[1])] = trim(pair[2])>

LVL 18

Expert Comment

ID: 37035041
This finds the position in the listusing | & ~ as delimiters, then gets the next list item
<cfset str = 'RESPONSE~101. Your transaction was successfully approved.|TRANSACTIONID~17514157|APPCODE~TAS331|APPMSG~ NO MATCH |AVSRESPONSE~No Match|CSCRESPONSE~Match|' />
<cfset pos = listFind(str, 'TRANSACTIONID', '|~') />
<cfset tid = listGetAt(str, pos+1, '|~') />

Open in new window

LVL 18

Expert Comment

ID: 37035047
If the list was always exactly the same, I would just do this

<cfset str = 'RESPONSE~101. Your transaction was successfully approved.|TRANSACTIONID~17514157|APPCODE~TAS331|APPMSG~ NO MATCH |AVSRESPONSE~No Match|CSCRESPONSE~Match|' />
<cfoutput>#listGetAt(str, 4, '|~')#</cfoutput>

Open in new window

LVL 53

Expert Comment

ID: 37035066
If it's always guaranteed to always be the same, the approach above is slicker. But overall, the structure approach is more robust. Especially if you need to access multiple keys.  You can even use structure functions to test whether a specific key is present ie structKeyExists(result, "keyName")

Author Closing Comment

ID: 37035415
That works perfect. Thank you so much!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

621 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