Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

How parse this string into an array

Enthusiast ,
Nov 05, 2010 Nov 05, 2010

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

2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Nov 05, 2010 Nov 05, 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 m

...
Translate
Advocate ,
Nov 05, 2010 Nov 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 05, 2010 Nov 05, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 05, 2010 Nov 05, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 05, 2010 Nov 05, 2010

Myself, I would probably use REMatch to pull out an array of addresses and then walk through the addresses and use ListGetAt to break apart the various parts of each address -- if ListLen(address) EQ 4 pull street1, street2, city, postal code else if EQ 3 pull street1, city, postal code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 05, 2010 Nov 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 06, 2010 Nov 06, 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]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 08, 2010 Nov 08, 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#" />

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 16, 2010 Nov 16, 2010

Steve thats genius

so cool how it matches an arbitrary amount of matches

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 16, 2010 Nov 16, 2010

Regular expressions are very cool but they do take quite a bit of trial and error to get them right -- especially since the CF docs are very slim in this area. I usually start using a web tool I found and bookmarked: http://www.txt2re.com/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 16, 2010 Nov 16, 2010

Regular expressions are very cool but they do take quite a bit of trial and error to get them right -- especially since the CF docs are very slim in this area. I usually start using a web tool I found and bookmarked: http://www.txt2re.com/

Interesting.

I don't mean to disagree with you, but I find the CF docs complete and easy to understand.  And 80% of basis for me to become fluent in CF's implementation of regexes, and sufficient to understand - if not be 100% comfortable with - the concepts behind various other regex engines.

Whereas I find that txt2re site to be gibberish, and close to completely unhelpful, despite understanding exactly what it's doing.

I guess it's all down to how one's brain works!

Cool.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 16, 2010 Nov 16, 2010

And to be fair, in some cases, a feature is something CF embeds, so it

makes sense for CF to talk about how it embeds the feature, but then

'shell out' to other docs. Like ORM for example. The docs cover CF

functions, CFCs, etc, but HQL is mentioned briefly since it isn't CF

specific and there are docs elsewhere. Regex is a good example of

this. You can find whole books just on regex itself.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 16, 2010 Nov 16, 2010

True, I just wish Adobe (Allaire, Macromedia, or whoever was responsible) kept the regular expression syntax 100% compatible with one of the well published and well documented flavors. Most of the time I burn debugging regular expressions deals with porting a working expression from JavaScript or php syntax to ColdFusion syntax.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2010 Nov 17, 2010

You mean like this: http://jakarta.apache.org/oro/api/ ?

That's the one they use.  Is that enough docs for you? šŸ˜‰

It's not something they wrote.  And PCRE regexes are a fairly well-established flavour of regexes.

Note: the implementation they use is - from memory - 2.0.6, which is from back in 2002...

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 17, 2010 Nov 17, 2010

I'll take a look. Thanks. Quick question for you though, how did you find this doc? Was it referenced somewhere in the CF docs?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2010 Nov 17, 2010

I was lobbying Adobe to get the regex engine overhauled because it lacks a bunch of stuff other engines offer.  As part of this, I had a dig around under the hood of CF (having been guided in that direction by someone else), and found the relevant jars.

BTW: you might want to go and vote for this: http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=76175

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 17, 2010 Nov 17, 2010

I was lobbying Adobe to get the regex engine overhauled

because it lacks a bunch of stuff other engines offer. As

I have never used ORO directly. Any idea if the lacking features are because of limitations in the underlying library or just CF's implementation?

-Leigh

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2010 Nov 17, 2010

Dunno.  I would have expected CF to just pass the stuff straight to the underlying system, in a fashion similar to how it does with JDBC stuff.  What things in the ORO spec are missing?  Bearing in mind CF doesn't have the latest version.

To be honest, I use regexes a lot, and I've never found myself really going "man... I wish I had [some bell or whistle from PERL's regex engine]" or anything like that.

Bear in mind that as well as CF's own implementation, the native Java implementation is also available too...

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2010 Nov 18, 2010
To be honest, I use regexes a lot, and I've never found myself really going "man... I wish I had [some bell or whistle from PERL's regex engine]" or anything like that.

Hmm.. then what did you mean by ...? šŸ˜‰

I was lobbying Adobe to get the regex engine overhauled

because it lacks a bunch of stuff other engines offer.

Honestly my regular expression skills are pretty lame. So I am taking your word for it. The one limitation I know of from other blogs is CF's implementation does not support negative look-behinds.  I wondered if that was due to a limitation in ORO.

Also, I have always assumed replace() and replaceNoCase() used regular expressions internally. Since there are few peculiarities in behavior there (like replacing double values ",,"), I wondered if was just a bad a expression or a regex limitation of some kind. Granted I am just guessing that is how replace() works internally. I could be way off base..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 18, 2010 Nov 18, 2010

To be honest, I use regexes a lot, and I've never found myself really going "man... I wish I had " or anything like that.

Hmm.. then what did you mean by ...?

>> I was lobbying Adobe to get the regex engine overhauled

>> because it lacks a bunch of stuff other engines offer.

There's a substantial difference between •needing• something and •wanting something just in case•. Negative look-behinds - as you say - is one of those.

Also - being on their prerelease programme - I also try to consider people's potential needs beyond my own.

A case-in-point of my urgency (or lack-thereof) for its inclusion is that I don't even know if Java's implementation supports 'em. I've never actually had the •need• to check...

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2010 Nov 18, 2010

Also - being on their prerelease programme - I also try to

consider people's potential needs beyond my own.

Makes sense.

 

I don't even know if Java's implementation

supports 'em. I've never actually had the •need• to

check...

For what it is worth, it does. That is what I eventually ended up using in a few cases. Of course my expressions were cobbled together from other sources. So I could not say whether the same results could be achieved without using negative look-behinds or not.

http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 19, 2010 Nov 19, 2010
LATEST

Perl's implementation of regex is as close to a standard as exists. CF implements that, as far as I can tell. Your real complaint should be with other languages that don't use a Perl-compatible regex syntax.

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on

GSA Schedule, and provides the highest caliber vendor-authorized

instruction at our training centers, online, or onsite.

Read this before you post:

http://forums.adobe.com/thread/607238

Dave Watts, Eidolon LLC
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources