Ask Ben: Parsing Nested Lists With A Single Delimiter In ColdFusion

Posted September 22, 2009 at 10:17 AM

Tags: ColdFusion, Ask Ben

Ben, How would I go about splitting a list into multiple lists? For example I have a list:

07/08/2009|1,573,067.20|8/8/2009|1,563,000.20

This list can be infinite in values but will always contain a date/amount combination. I need to split these into lists of 2 one date/amount lists. Seems like it should be easy enough to do but I think I have been looking at it for too long. Any help you can give would be greatly appreciated.

Since you have nested lists (a list of lists) but only a single delimiter, we cannot look at this value as list just yet; at least, not in the most usable way. To make this data usable, we have two options. Since you hinted at wanting to split this into multiple lists, I'll explore, as our first option, looking at this list of lists as a pattern in which we have a date value followed by a pipe followed by an amount value. Or, to abstract it out even more, a non-pipe value followed by a pipe followed by a non-pipe value. With this pattern mentality, we can then quite easily extract all of the sub-lists from this master list despite the fact that there is only one delimiter:

 Launch code in new window » Download code as text file »

  • <!---
  • Create the data set (I am using string concatenation here
  • only for display reasons).
  • --->
  • <cfset data = (
  • "07/08/2009|1,573,067.20|8/8/2009|1,563,000.20|" &
  • "9/8/2009|1500000|10/8/2009|800000"
  • ) />
  •  
  • <!---
  • Right now, we have a HUGE list that has a single delimiter
  • (|). As such, we cannot really think of this as a list yet.
  • But, we can think of it as a pattern; a date followed by an
  • amount, separated by a pipe. With this mentality, we can
  • use the reMatch() method to extract the sub-lists from our
  • larger list.
  •  
  • Basically, the pattern is simple: a value that does not
  • contain the pipe (our date) followed by the pipe, followed
  • by a value that does not contain the pipe (our amount).
  • --->
  • <cfset pairs = reMatch(
  • "[^|]+\|[^|]+",
  • data
  • ) />
  •  
  • <!---
  • At this point, our pairs contains a bunch of smaller lists,
  • each with two values: our date and our amount. This list is
  • pipe-delimited and can be easily parsed:
  • --->
  • <cfloop
  • index="pair"
  • array="#pairs#">
  •  
  • Date: #listFirst( pair, "|" )#,
  • Amount: #listLast( pair, "|" )#
  • <br />
  •  
  • </cfloop>

Once we extract the sub-lists from the master list, our resultant array contains many small values, each of which is in the form of "date-pipe-amount". These values can then be iterated over and treated as a clean, pipe-delimited list. And, when we run the above code, we get the following output:

Date: 07/08/2009, Amount: 1,573,067.20
Date: 8/8/2009, Amount: 1,563,000.20
Date: 9/8/2009, Amount: 1500000
Date: 10/8/2009, Amount: 800000

This works quite nicely.

If you are not tied to sub-lists, our second, perhaps cleaner, option would be to take this list and split it into a single array; this method will be faster and even a bit more accessible as it does not require any understanding of regular expressions:

 Launch code in new window » Download code as text file »

  • <!---
  • Create the data set (I am using string concatenation here
  • only for display reasons).
  • --->
  • <cfset data = (
  • "07/08/2009|1,573,067.20|8/8/2009|1,563,000.20|" &
  • "9/8/2009|1500000|10/8/2009|800000"
  • ) />
  •  
  • <!---
  • Split the entire data value into an array on the pipe
  • delimitter. Once we do this, our date/amount pairs will
  • be in successive indexes of the resultant array.
  • --->
  • <cfset parts = listToArray( data, "|" ) />
  •  
  • <!---
  • At this point, our entire data list has been broken out into
  • tokens. Loop over the array, incrementing by 2 since each two
  • indeciis are related.
  • --->
  • <cfloop
  • index="index"
  • from="1"
  • to="#arrayLen( parts )#"
  • step="2">
  •  
  • Date: #parts[ index ]#,
  • Amount: #parts[ index + 1 ]#
  • <br />
  •  
  • </cfloop>

As you can see, once we split the master list on the pipe-delimiter, our array now contains all the list items in which are sub-lists are represented by successive array indices. Looping over the array by increments of two allows us to easily grab those sub-list values. And, when we run the above code, we get the same output:

Date: 07/08/2009, Amount: 1,573,067.20
Date: 8/8/2009, Amount: 1,563,000.20
Date: 9/8/2009, Amount: 1500000
Date: 10/8/2009, Amount: 800000

I am a huge fan of regular expressions, but they are probably not the right solution for this job. I would most likely end up going with method two. I hope this helps!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 22, 2009 at 4:44 PM // reply »
4 Comments

@Ben
This is interesting because it is one of those types of things that could be solved in many different ways. I would've probably taken the low tech approach of creating a new array and then looping through the list and stuffing values in their respective columns based on whether the index of the array was even or odd - which would probably work because we've only got two columns in this example. Any post that makes you stop and say "How would I do that?" is a good one!


Sep 23, 2009 at 8:02 AM // reply »
7,572 Comments

@Andy,

Yeah - very true - there are number of ways to solve this kind of problem; that's part of the reason why I try to never view a question as too simple; often times, just the act of answering it generally precipitates new thoughts on how to solve it.


JC
Sep 23, 2009 at 6:20 PM // reply »
7 Comments

I probably would have used MOD... if list item number is even, name, odd, value... something like that. But this is probably a better way. :)


Sep 24, 2009 at 9:18 AM // reply »
7,572 Comments

@JC,

The MOD operator certainly rocks though :)


Oct 21, 2009 at 9:59 PM // reply »
51 Comments

@Ben,

Awesome solutions!

I would have never thought to look at this as a pattern. I like the first approach, mainly because I think regex is amazing.


Oct 31, 2009 at 4:05 PM // reply »
7,572 Comments

@Andrew,

Regex definitely is amazing :)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »