Using a Compound List to Simulate a Compound Boolean Statement
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
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.
<cfset match_count= 0>
<cfloop index="andlist" list="#enrollment_rule#" delimiters="|">
<!--Or list is now another list (comma-delimited) of trait values-->
<cfloop index="orlist" list="#andlist#" delimiters=",">
<cfif ListFind(student_traits,orlist) GT 0>
<cfset match_count= match_count+1>
</cfif>
</cfloop>
</cfloop>
<cfif rules_ands EQ match_count>
<cfset eligible= "Yes">
<cfelse>
<cfset eligible= "No">
</cfif>
