| By Bruce Van Horn | Article Rating: |
|
| April 8, 2002 12:00 AM EDT | Reads: |
9,271 |
Two questions I received this month made me realize just how often we overlook the power of Lists and the many List functions in CF.
Many times, we look at a string or other piece of data and don't even realize that what we're looking at is actually a list. Once we realize that something can be treated as a list, CF gives us many ways of using or manipulating that data. Here are two perfect examples:
Q: We have an e-mail news-letter that we send out to those who subscribed at our Web site. All they have to do is give us their e-mail address. I want to find out how many unique domains are represented in our subscription list. In other words, for "john.smith@somecompany.com", how can I parse out the domain ("somecompany.com") from the user ("john.smith") in an e-mail address?
A: There are several easy ways to do this. The easiest way, perhaps, is to treat the e-mail address as a list that's delimited by the "@" character. Most people think of lists as being delimited only by commas or tabs and usually containing more than two data elements. CF, however, allows you to specify any character as the delimiter, so virtually any data string can become a list in the eyes of CF. I'll show you how to count the number of unique domains quickly, but I'll also encourage you to consider changing the way you store your e-mail address in your database to make this easier in the future.
First (see Listing 1), run a query to retrieve all your e-mail addresses. Next, create an empty list variable to hold the unique domain names. Next, loop over the query, treat each e-mail address as a list, and pull out the second element (the domain) in the list. If that domain isn't already in the list, append it to the list. Last, output the count of items (using ListLen()) in the domain list.
Now for my other recommendation: most of us simply create a column in our database called "E-mail" into which we stick our e-mail addresses. However, most of us also want some kind of reporting, as you've mentioned, so we know how many domains are represented in our list or, more likely, how many people there are from each domain (you'll quickly see how many of your subscribers are from AOL!). For this reason, you might want to consider breaking your one "E-mail" column into two database columns: "E-mail_Name" and "E-mail_Domain". You don't have to change anything on your Web form - let users enter their e-mail address in a single field. It's still very easy to insert into the database (see Listing 2), and it also makes it easy to pull your report using a query (see Listing 3).
Q: My application needs to add data to another company's application (it's written in PHP). I understand how to post the data to their site (I use the CFHTTP tag to post the data), but the site gives me back return codes that are one long string with the variables separated by the "&" character and I'm not sure how to get the data I need out of the string since it often comes back in a different order for each post. Their tech support people haven't been very helpful. Any suggestions for how I can get this data?
A: Yes. This is a common way of giving return codes back to a remote application, and getting the data can be very easy. What you want to do is break the single string down into the individual variable/value pairs that make up the string. Then you want to turn those pairs into CF variables that you can use in the logic of your application. Your return string may look something like this: "id=28734 &status=success&message=record was added successfully&add_date= 3/1/02".
Here are the steps to take (see Listing 4): first, initialize an empty structure to hold the variables/values returned by the remote application. Since you're using CFHTTP to post to the other site, their return string is coming back to CF as a variable named CFHTTP.FileContent. As you indicated, that string is a list delimited by the "&" character. Therefore, you need to loop over this list to find each variable/value pair. As you loop, you need to break each pair into its appropriate part (a variable name and its value). This is nothing more than another list delimited by a "=" sign. The value in front of the equal sign (position 1 of the list) is the variable name. The value in position 2 is the actual value of that variable. Set each one into its own variable and then dynamically name keys in your structure with the appropriate name/value. Now, regardless of the order in which they came back to you, you can test for the existence/value of the variables you were expecting.
Please send your questions about ColdFusion (CFML, CF Server, or CF Studio) to AskCFDJ@sys-con.com. And please visit our archive site at www.NetsiteDynamics.com/AskCFDJ.
Published April 8, 2002 Reads 9,271
Copyright © 2002 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Bruce Van Horn
Bruce Van Horn is president of Netsite Dynamics, LLC, a certified ColdFusion developer/instructor, and a member of the CFDJ International Advisory Board.
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Adobe Betas Target RIAs and Cloud Computing
- Adobe MAX 2009 Online
- Thinking of Flex in London
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe & Salesforce Cut Cloud Deal
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Is Microsoft as Free as Open Source?
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- Adobe Flex Developer Earns $100K in New York City
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF



































