Copy link to clipboard
Copied
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
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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]
Copy link to clipboard
Copied
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#" />
Copy link to clipboard
Copied
Steve thats genius
so cool how it matches an arbitrary amount of matches
Copy link to clipboard
Copied
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/
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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..
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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