Pulling a transaction number from a string (coldfusion)

Posted on 2011-10-26
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
    LVL 51

    Accepted Solution

    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

    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

    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 51

    Expert Comment

    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

    That works perfect. Thank you so much!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
    Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now