Search code examples
coldfusioncoldfusion-9coldfusion-10

How to split a list separated by ";" into sub lists in ColdFusion


I need to split one list, delimited by ;, into multiple sub lists. Can I do it without converting it into an array in ColdFusion?

Example: My_list contains:

[10043,10044,10045,10046:2,5,3,1;3453,2167:1,0;2346,8674,9043,7543,6453:0,4,2,0,1]

I need:

My_list1 = [10043,10044,10045,10046:2,5,3,1]
My_list2 = [3453,2167:1,0]
My_list3 = [2346,8674,9043,7543,6453:0,4,2,0,1]

... and so on.


Solution

  • You don't need to "do" anything. A list is just a delimited string. So if you want to set those (very poorly named, IMO) variables, it's just a matter of:

    <cfset fullList = "10043,10044,10045,10046:2,5,3,1;3453,2167:1,0;2346,8674,9043,7543,6453:0,4,2,0,1">
    <cfset varIndex = 0>
    <cfloop index="subList" list="#fullList#" delimiters=";">
        <cfset "My_list#++varIndex#" = subList>
    </cfloop>
    <cfdump var="#variables#">
    

    I seriously wouldn't use dynamic variable names like that though, I'd use an array.