我试图遍历两个列表,一个是预制列表,一个是列表文字。这样可能吗?
伪代码示例:
list(list1 APPEND 0 1 2 3 4)
foreach(item IN LISTS ${list1} 5 6 7 8 9)
message(${item} ${#other variable})
endforeach(item)
# prints out
0 5
1 6
... etc
答案 0 :(得分:0)
要同时遍历多个列表,可以在它们的索引上使用foreach
循环。然后,在循环主体中通过该索引访问列表的元素:
# Setup content of the lists somehow
set(list1 0 1 2 3 4)
set(list2 5 6 7 8 9)
list(LENGTH list1 n_elems) # Total number of the elements in the every list
math(EXPR last_index "${n_elems}-1") # The last index in the every list
# Now iterate over indicies
foreach(i RANGE ${last_index})
list(GET list1 ${i} elem1) # Element in the first list
list(GET list2 ${i} elem2) # Corresponded element in the second list
# Do something with elements
message("${elem1} ${elem2}")
endforeach()