Search code examples
listloopscoldfusioncoldfusion-9

How To Loop Through Two Lists?


I need to join the output of two separate lists together to output in a CFMAIL, and I'm wondering what the best way to approach this is.

I have two form fields: first_name and last_name

The fields have up to 5 names in each. I need to loop through those names and join the first and last names, then output them to unordered list. I am having trouble visualizing what the right approach to accomplish this is.

Can someone suggest a method in CFML (I don't know CFSCRIPT very well).

Thanks!

EDIT: I should have added that both fields will always have the exact same number of entries. Thanks to all that answered -- proof that there are a lot of ways to skin a cat :)


Solution

  • I would do something like

    <cfloop from="1" to="#ListLen(firstnames)#" index="i">
    #ListGetAt(firstnames,i)# #ListGetAt(lastnames,i)#<br>
    </cfloop>
    

    If this were a list of 5000 you would be better off putting it in a structure or an array, but for a list of ~5 this should be sufficient.