Friday, December 8, 2006

Using a Compound List to Simulate a Compound Boolean Statement

I was once asked to build an application that would use the demographic information submitted by would-be students to ensure that they were eligible to participate in certain social and cultural discussion sections. Each discussion section would have a number of enrollment rules that would not only determine which students were eligible, but would also set a limit on how many students could enroll for that discussion under that rule. For example, one of these enrollment rules would say "allow 5 students who are freshman or sophomores and were born in the U.S. and are atheists or agnostics and who grew up in a rural or suburban area to enroll in this discussion."

I knew I had to find a way to handle these complex evaluations in a flexible manner without resorting to multiple layers of if/else statements. Fortunately, I figured out a way to use compound lists to evaluate a student`s eligibility.

For any non-ColdFusion programmers who might be listening, ColdFusion has a number of functions for processing lists, lists being strings that can be divided into separate pieces by a delimiter character, usually a comma. For example, the function ListGetAt("red,blue,green,yellow",2,",") would return the value "blue" (the second item in the string where each item is separated/delimited by a comma). Other functions in ColdFusion can make use of lists, such as the tag, which can loop through each item in a list one at a time.

Because it`s possible to designate any characters (or even a string of characters) as the delimiter in a list function, the same string can be evaluated as different lists, and I used this fact to create my evaluation routine. I wrote code that translated the enrollment rules into lists where an AND operator was represented by a pipe (|) character and the OR conditions were separated by commas. So the enrollment rule I used earlier was translated into:

freshman,sophomore|USborn|atheist,agnostic|rural,suburban

I then wrote code to write all of the demographic data that was entered by the student into a simple comma-delimited list.

To determine if a student was eligible to enroll in a discussion, the evaluation code would count the number of AND operators in the enrollment rule (in this example, 4). The code would then run an outer loop that would loop through all of the AND operators one at a time. Inside this outer loop would be a second loop that would loop through all of the OR conditions one at a time. If the ListFind function determined that any of the OR conditions in the enrollment rule were present in the demographic list, a counter variable would be incremented. If, at the end of both loops, the counter variable value matched the number of AND operators in the enrollment rule, that meant that the student was eligible. Here is the code:

For more information on ColdFusion functions regarding lists, check out the List functions at cfQuickDocs, an AJAX-powered ColdFusion documentation site.