Need to Parse one value from two lines of data

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Need to Parse one value from two lines of data

    Hi,

    I'm doing a cfhttp request to a shipping rate provider and the response I get
    back looks like this:

    GND, NAS, SDS, EXP
    $6.50, $12.25, $7.85, $14.00

    The problem is that I only need to display to my end user the first bit of
    data on the second line.
    I thought I would be ok by parsing out the response and setting my delimiter
    to a comma, but unfortunately the end of the first line doesn't end with a
    comma so my code considers "EXP
    $6.50" to be the value at position 4.

    Anyone have any ideas on how I can just pull out the $6.50?




    <cfhttp method="post" url="http://www.weburl.com">

    <CFOUTPUT>
    <cfhttpparam name="fromZip" type="formfield" value="#Zip_VendorInfo#">
    <cfhttpparam name="toZip" type="formfield"
    value="#GetShippingInformation.OrderShippingZip#">
    <cfhttpparam name="weight" type="formfield"
    value="#NumberFormat("#ShippingWeight#")#">
    <cfhttpparam name="acctNum" type="formfield" value="#AccountNumber#">
    </cfoutput>
    </cfhttp>

    <!--- here's the value being sent back from the server --->
    <cfset api_response=cfhttp.fileContent>

    <cfif isdefined("api_response")>
    <!--- This is where we parse out all the necessary responses --->
    <CFLOOP INDEX="ListElement" LIST="#api_response#" DELIMITERS=","></CFLOOP>

    <cfset numberDelims = ListLen(api_response, ",")>

    <cfset MyList = api_response>
    <cfset MyArray = ListToArray(MyList, ",")>

    <cfloop index="Element" from="4" to="5">
    <cfoutput>
    <cfswitch expression="#Element#">
    <cfcase value="4"><cfset GNDRate = #MyArray[Element]#></cfcase>
    </cfswitch>
    </cfoutput>
    </cfloop>

    Resp: <cfoutput>#GNDRate#</cfoutput>

    </CFIF>

    Purple Haze Guest

  2. Similar Questions and Discussions

    1. How do I parse a txt File and write data into aDatabase
      I'm trying to do an insert off of your example. It keeps throwing an error that it doesn't understand "fields1." Can you tell me where I might be...
    2. Data File, turn fields on mulitple lines into records on one li ne.. .
      Please bottom post... John just provided a good one. Optionally if all you care about is determining whether a line contains a string it might...
    3. Using perl to parse specific lines by date...
      While I've already done this with a simple shell script using grep, I was trying to figure out how I can do the same thing in perl. I have an...
    4. Is it possible to parse specific data from within large notes field?
      I have a large DB with a text field that may have a fax number somewhere in it. I want to be able to extract into a separate field, the first 15...
    5. Can you parse data by an undefined pattern ?
      Hi JS if there is consistency within the inconsistency then; I usually attack this kind of situation on a progressive yield hope and write a...
  3. #2

    Default Re: Need to Parse one value from two lines of data

    Consider treating the reply as a $-delimited list. Your second list element would be 6.50,

    <CFSET temp = ListGetAt(api_response, 2, "$")>
    <CFSET price = Mid(temp, 1, Len(temp) - 1)>
    jdeline Guest

  4. #3

    Default Re: Need to Parse one value from two lines of data

    thanks that did the trick
    Purple Haze Guest

  5. #4

    Default Re: Need to Parse one value from two lines of data

    If the result are returned as rows of comma-delimited lists, you should
    parse them as such, using CR & LF as a row delimiter and a comma as the
    column delimiter. This method is not dependant on the specific format of
    the individual column values.

    <cfset rowDelim = chr(10)&chr(13)>
    <cfset row = ListGetAt(api_response, 2, rDelim)>
    <cfset columns = ListToArray(row)>
    <cfset GNDRate = columns[1]>


    _jt Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139