[CMake] Iterating empty elements in a list

Robert Dailey rcdailey.lists at gmail.com
Tue Aug 6 16:25:47 EDT 2013


Sorry for the spam, after posting a solution dawned on me. Here is
what I did for anyone else that might need a solution (or care to
propose a better one):


function( replace_empty_elements out_list in_list )
    foreach( item IN LISTS in_list )
        if( item STREQUAL "" )
            set( item_val "<EMPTY>" )
        else()
            set( item_val ${item} )
        endif()

        list( APPEND formatted_list ${item_val} )
    endforeach()

    set( ${out_list} "${formatted_list}" PARENT_SCOPE )
endfunction()


Use it like so:

set( inlist ";;test;" )
replace_empty_elements( outlist "${inlist}" )
message( "outlist: ${outlist}" )

Output:

outlist: <EMPTY>;<EMPTY>;test;<EMPTY>

On Tue, Aug 6, 2013 at 3:10 PM, Robert Dailey <rcdailey.lists at gmail.com> wrote:
> Just to be clear, I can't use foreach():
>
> foreach( <var> IN LISTS <list> )
>
> I am doing a "yielded" approach to looping by using list( GET ) and
> list( LENGTH ) in combination. So I would need the list() functions to
> acknowledge empty elements.
>
> On Tue, Aug 6, 2013 at 3:04 PM, Robert Dailey <rcdailey.lists at gmail.com> wrote:
>> I've seen that list(LENGTH) will ignore empty elements in a list. Example:
>>
>> 1;2;;4;5
>>
>> Length will be 4 here instead of 5. I was trying to come up with a way
>> to modify the list as a string prior to iterating it to give empty
>> elements some dummy value, so that the above example would appear as:
>>
>> 1;2;<empty>;4;5
>>
>> However, this isn't going to work with string( REGEX REPLACE ) as the
>> modifications are not included in the continuation of the regex search
>> (I think positive lookbehind is needed here?). Basically this case
>> cannot be converted:
>>
>> ;;;
>>
>> to:
>>
>> <empty>;<empty>;<empty>
>>
>> Anyone know how I can iterate empty elements in a list?


More information about the CMake mailing list