Skip to main content
nikos101
Inspiring
November 5, 2010
Answered

How parse this string into an array

  • November 5, 2010
  • 2 replies
  • 3095 views

ANyone see an easy way to parse this string into an array of type

[street 1, street 2, city, post code]

the string:

'

[

[address="Paris  Ltd,5 North Street, Athens, SW1X 9LA"]

[address="Paris  Ltd,5 North Street, Athens, SW1X 9LA"]

]

'

thx

    This topic has been closed for replies.
    Correct answer JMF3

    If you just want the address as a single entry like myArray[1] = "Paris Ltd,5 North Street, Athens, SW1X 9LA", then try this:

    <cfset myArray = [] />

    <cfloop list="#theString#" delimiters="#chr(10)#" index="line">

    <cfif listLen(line)>

    <!--- there is content in the line, so just add the bit between " to the array --->

    <cfset arrayAppend(myArray, listGetAt(line, 2, '"')) />

    </cfif>

    </cfloop>

    If you actually want the various elements broken out, then you would need an array of structs, so that you get myArray[1].street1 = "Paris Ltd":

    <cfset myArray = [] />

    <cfloop list="#theString#" delimiters="#chr(10)#" index="line">

    <cfif listLen(line)>

    <!--- there is content in the line, so parse it out into a struct and add the struct to the array --->

    <cfset addressList = listGetAt(line, 2, '"') />

    <cfset addressStruct = { street1=listFirst(addressList), street2=listGetAt(addressList, 2), city=listGetAt(addressList, 3), postCode=listLast(addressList) } />

    <cfset arrayAppend(myArray, addressStruct) />

    </cfif>

    </cfloop>

    Note:  As Steve pointed out, you may need to test for listLen() on the addressList to determine whether or not there is a street2 entry in any given line.

    2 replies

    JMF3Correct answer
    Participating Frequently
    November 5, 2010

    If you just want the address as a single entry like myArray[1] = "Paris Ltd,5 North Street, Athens, SW1X 9LA", then try this:

    <cfset myArray = [] />

    <cfloop list="#theString#" delimiters="#chr(10)#" index="line">

    <cfif listLen(line)>

    <!--- there is content in the line, so just add the bit between " to the array --->

    <cfset arrayAppend(myArray, listGetAt(line, 2, '"')) />

    </cfif>

    </cfloop>

    If you actually want the various elements broken out, then you would need an array of structs, so that you get myArray[1].street1 = "Paris Ltd":

    <cfset myArray = [] />

    <cfloop list="#theString#" delimiters="#chr(10)#" index="line">

    <cfif listLen(line)>

    <!--- there is content in the line, so parse it out into a struct and add the struct to the array --->

    <cfset addressList = listGetAt(line, 2, '"') />

    <cfset addressStruct = { street1=listFirst(addressList), street2=listGetAt(addressList, 2), city=listGetAt(addressList, 3), postCode=listLast(addressList) } />

    <cfset arrayAppend(myArray, addressStruct) />

    </cfif>

    </cfloop>

    Note:  As Steve pointed out, you may need to test for listLen() on the addressList to determine whether or not there is a street2 entry in any given line.

    nikos101
    nikos101Author
    Inspiring
    November 6, 2010

    i think steves regex approach is the easiest to understand but I can't figure out how to split

    '[[foo][foo][foo].....[foo]]'

    into an array of [foo]

    I can then use regex to get out the parts of [foo]

    Legend
    November 8, 2010

    Here is a start:

    <cfsavecontent variable="variables.info">
      [
       [address="Paris 1 Ltd,1 North Street, Athens, SW1X 9LA"]
       [address="Paris 2 Ltd,2 North Street, Athens, SW1X 9LA"]
      ]
    </cfsavecontent>

    <cfset variables.expression = "\[address=""[^""]*""\]" />

    <cfset variables.a = REMatch(variables.expression,variables.info) />
    <cfdump var="#variables.a#" />

    Legend
    November 5, 2010

    This seems like a class project. Not meaning to be a butt (that's just me), if you intend on being ColdFusion programmer, you should really figure this one out yourself. There are several ways to tackle this. I hire web developers and I often give test like this to candidates -- if they can't prove to me they can do it in a reasonable timeframe, they don't get hired.

    nikos101
    nikos101Author
    Inspiring
    November 5, 2010

    No no I'm a programmer but only lightly use CF, just there may be an way of doing this without having to dig through docs


    regards
    Inspiring
    November 5, 2010

    It shouldn't take too much digging. There's a page that lists all the array functions.  And another that list all the string functions.

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09f0b-8000.html

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-6a3a

    Or just GOOGLE the subject line of your own post, eg:  "CF How parse this string into an array".

    There's a number of answers on the first page of results; or at least enough to get you going in the right direction.

    --

    Adam