Bill Hoffman wrote:
Alan W. Irwin wrote:
On 2008-05-07 09:12-0700 Alan W. Irwin wrote:

foreach(element ${list_example})
  message("${element}")
endforeach(element list_example)

The result is

input list_example = 1;;;2;3;4;5
individual elements are listed as follows:
1
2
3
4
5

Clearly, the empty elements are (incorrectly) being skipped in the foreach
loop.


Nothing we can do about the foreach. The ${list_example} is being expanded before foreach gets the list. The only thing we could do is change the signature of foreach to take a variable name as well as what it takes now.

-Bill

We actually talked about this when I created policy CMP0007. Brad had this to say:


The foreach command does see empty items:

  foreach(x "" "" a "" b)

will give x each value including the empty string.

However,

  set(ITEMS "" "" a "" b)
  foreach(x ${ITEMS})

will skip empty ones.  Variable expansion without quotes has *always*
removed empty items.  It occurs before the foreach command even sees
them and is consistent across all commands.  We should not change this.

The difference is that the list command deals with lists by their
variable name, not in expanded form.  The way to deal with this in
foreach is to create a new "list" mode that takes a list by name.  So,
something like

  foreach(x IN ITEMS)

will iterate over the entries of the list contained in the variable
ITEMS, including empty ones.  This does not require a policy.  It just
means people cannot iterate through argument lists whose first entry is
"IN" (already the case for "RANGE").


BTW, I have fixed the list(SORT to handle empty correctly.


-Bill

_______________________________________________
CMake mailing list
CMake@cmake.org
http://www.cmake.org/mailman/listinfo/cmake

Reply via email to