Skip to main content
October 2, 2006
Answered

How to split a list ??

  • October 2, 2006
  • 7 replies
  • 2519 views
Can you tell me how to split a list in ColdFusion ? I tried to look but did not find any in the CF reference..
    This topic has been closed for replies.
    Correct answer
    See below.

    7 replies

    Inspiring
    October 2, 2006
    Just make sure you test that answer for lists with an odd number of elements.
    October 2, 2006
    Thank you for your replies.........That really helped me ........

    Thanks
    Vamsi
    Inspiring
    October 2, 2006
    listlen() would be part of the code if I had to split a list into two, almost equal halves.
    Inspiring
    October 2, 2006
    > I want to input a list and want to divide it into two equal(almost equal) halfs..

    There is no in-built way to do this (which bites).

    But you can do this to split a list:

    1) work out at which list item you want to split it
    2) prepend some other delimiter to the item at that position in the list
    3) use that other delimiter as the delimiter for a listFirst() or
    listLast() [etc] call.

    --
    Adam
    October 2, 2006
    I want to input a list and want to divide it into two equal(almost equal) halfs..

    ListFirst(), ListLast(), ListToArray() does'nt help

    Correct answer
    October 2, 2006
    See below.
    Inspiring
    October 2, 2006
    and the loop for of <cflist list="#listVar#"...>

    CORRECTION

    and the list from of cfloop - <cfloop list="#listVar#" ...>




    I hate when I get interrupted and don't proof read properly.
    Inspiring
    October 2, 2006
    What do you want to split it into?

    There is a robust collection of list functions in CF. ListGetAt(),
    ListFirst(), ListLast(), ListToArray() and the loop for of <cflist
    list="#listVar#"...>

    I suspect ListToArray() may be what you want, but check the
    documentation for the function category of List to see the complete list
    of list functions.