0 Favourites

How do I use the contents of a string as a calculation?

  • Hi all!

    Is it possible to have the contents of a string variable used as a calculation? For example if I had a string that read "1+3+4" is there any way that string could be processed to express "8"?

    My string is generated through a combination of RegexReplace and replace scripts (

    replace(RegexReplace(testObject.inputVarOne,"([[a-zA-Z][a-zA-Z0-9]*,?)","g",""),",","+")[/code:yvh8elm2]) eventually turning a string of values into something more akin to a mathematical formula like the one shown above. Is there a way to turn that outputted string value into a calculation that construct2 can read 8 instead of 1+3+4?
    
    ---- update
    
    Here is what I have attempted so far: 
    I have created a string variable and set it to the above formula, meaning the string value is literally "1+2+3" I created a second, number variable and set it to the test variable I just made, and unfortunately instead of outputting "6" as expected, it simply output "1".  Can anyone offer any insight?
  • for your particular example, you will do:

    local static var sum = 0

    For

    from 0 to tokencount("1+2+4","+")

    >> Add to: sum: int(tokenat("1+2+4",loopindex,"+")

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • for your particular example, you will do:

    local static var sum = 0

    For

    >> from 0 to tokencount("1+2+4","+")

    >>>> Add to: sum: int(tokenat("1+2+4",loopindex,"+")

    Thank you! This worked perfectly

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
99 10,139
karshinkoff's avatar
karshinkoff
Unread hot topic
56 4,734
MPPlantOfficial's avatar
MPPlantOfficial
Unread hot topic
76 9,273
Pandadoor's avatar
Pandadoor