Ask Ben: Parsing Nested Lists With A Single Delimiter In ColdFusion
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:
<!---
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:
<!---
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!
Want to use code from this post? Check out the license.
Reader 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!
@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.
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. :)
@JC,
The MOD operator certainly rocks though :)
@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.
@Andrew,
Regex definitely is amazing :)