Discussion:
[CMake] doing simple math in cmake or useless foreach( RANGE )
Hendrik Sattler
2009-05-19 21:29:09 UTC
Permalink
Hi,

either I cannot find it or it's not there: doing simple mathematic things like
substract 1 from a variable.

If that's not present, I do not understand how
LIST( LENGTH MYLIST COUNT )
FOREACH ( index RANGE ${COUNT} )
list ( GET MYLIST ${index} entry )
....

will address non-present indexes in the list. I would the above statement
cover the range from 0 to COUNT-1 but it also cover COUNT itself like said in
the documentation. Does that actually make sense as the variable in the
documentation is called "total" but the loop count is "total+1"?

Currently I use a
if ( LESS )
work-around but that looks like a hack.

Any ideas?

HS
Matthew Woehlke
2009-05-19 21:54:09 UTC
Permalink
Post by Hendrik Sattler
either I cannot find it or it's not there: doing simple mathematic things like
substract 1 from a variable.
[snip]
Any ideas?
cmake --help-command math
??
--
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
--
Here endeth the rant... which I realize is not likely to Win Friends or
Influence People, especially the person to whom it was directed.
-- Charles Wilson (generalized)
Nicolas Desprès
2009-05-20 02:38:05 UTC
Permalink
On Tue, May 19, 2009 at 6:29 PM, Hendrik Sattler
Post by Hendrik Sattler
If that's not present, I do not understand how
LIST( LENGTH MYLIST COUNT )
FOREACH ( index RANGE ${COUNT} )
list ( GET MYLIST ${index} entry )
....
will address non-present indexes in the list. I would the above statement
cover the range from 0 to COUNT-1 but it also cover COUNT itself like said in
the documentation. Does that actually make sense as the variable in the
documentation is called "total" but the loop count is "total+1"?
I also experienced this problem and I don't like this behavior. I'm
more use to the traditional

for (i = 0; i < n; ++i)

instead of <=
--
Nicolas Desprès
Mats Kindahl
2009-05-20 05:21:29 UTC
Permalink
Post by Hendrik Sattler
Hi,
either I cannot find it or it's not there: doing simple mathematic things like
substract 1 from a variable.
If that's not present, I do not understand how
LIST( LENGTH MYLIST COUNT )
FOREACH ( index RANGE ${COUNT} )
list ( GET MYLIST ${index} entry )
....
will address non-present indexes in the list. I would the above statement
cover the range from 0 to COUNT-1 but it also cover COUNT itself like said in
the documentation. Does that actually make sense as the variable in the
documentation is called "total" but the loop count is "total+1"?
Currently I use a
if ( LESS )
work-around but that looks like a hack.
Any ideas?
I used:

list(LENGTH MYLIST _len)
math(EXPR COUNT "${_len}-1")
foreach(index RANGE ${COUNT})

but I also think that foreach() should assign integers in the range
0..${COUNT}-1 instead of 0..${COUNT}.

Just my few cents,
Mats Kindahl
Post by Hendrik Sattler
HS
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
http://www.cmake.org/mailman/listinfo/cmake
--
Mats Kindahl
Senior Software Engineer
Database Technology Group
Sun Microsystems
Loading...