I'm trying to write a CMake script that does the following:
Using the execute_process instruction reads the output of a command and stores it into a variable named 'STRING_VARIABLE'. The command that returns something that has both characters and digits in it's name. Something like this: RESULT-v1.2.8-...
I have read this value properly and displayed it on the terminal to confirm this. Now what I want to do is store the first three digits of this output and store them in 3 other variables: 'FIRST_DIGIT', 'SECOND_DIGIT' and 'THIRD_DIGIT'.
My logic was this: Using a counter count each time a digit is encountered in a variable name. Each time a digit is encountered store the digit in one of the three variables then increment the counter. The counter counts therefore from 0 to 2 and for each of these 3 values does a store.
Here is the script I wrote:
SET(COUNTER 0)
foreach(LETTER ${STRING_VARIABLE})
if(LETTER EQUAL '0,1,2,3,4,5,6,7,8,9')
if( COUNTER EQUAL 0 ) # if first digit is encountered
list(GET STRING_VARIABLE LETTER FIRST_DIGIT) # store it in FIRST_DIGIT
SET(COUNTER 1)
elseif( COUNTER EQUAL 1 ) # if second digit is encountered
list(GET STRING_VARIABLE LETTER SECOND_DIGIT) # store it in SECOND_DIGIT
SET(COUNTER 2)
else( COUNTER EQUAL 2 ) # if second digit is encountered
list(GET STRING_VARIABLE LETTER THIRD_DIGIT) # store it in THIRD_DIGIT
endif()
endif()
endforeach()
# To check the variables
#message("*****${STRING_VARIABLE}") # OK!
message("*****${FIRST_DIGIT}") # NOT OK :(
As I'm a total beginner in CMake I suppose my problem is at either of the two(or both): - When looping through the 'STRING_VARIABLE' I used foreach(LETTER) and since my string also contains digits the program may not see them. If that is the mistake with what else should I replace LETTER in order to get each character of the string? - In the first if where I check if the 'LETTER' is a digit. I think that is the correct syntax altough I'm not sure. Basically what I'm doing there is trying to check if the letter at each index is a digit.
The 'STRING_VARIABLE' prints ok as I said. However when I try printing the 'FIRST_DIGIT' or any other of the 3(second and third) I get an empty string as a result. Please help me understand what is wrong in my logic and what I'm doing wrong.
Please help me understand what I'm doing wrong. Thank you.
In case the format is know, you can use string(REGEX REPLACE ...).
Function:
Usage:
Result: