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  |  Permalink  |  Other Searches  |  Print Page



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

Reader Comments

Sep 22, 2009 at 4:44 PM // reply »
2 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 »
6,516 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 »
6,516 Comments

@JC,

The MOD operator certainly rocks though :)


Oct 21, 2009 at 9:59 PM // reply »
45 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 »
6,516 Comments

@Andrew,

Regex definitely is amazing :)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »