[solve] How do I compare using strings?

0 favourites
  • 5 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • solved. Thank you Ashley and dop2000

    I put the code if someone needs in the future.

    Paste directly as Function:

    {"is-c3-clipboard-data":true,"type":"events","items":[{"functionName":"Compare","functionDescription":"","functionCategory":"","functionReturnType":"string","functionIsAsync":false,"functionParameters":[{"name":"source","type":"string","initialValue":"","comment":""}],"eventType":"function-block","conditions":[],"actions":[],"children":[{"eventType":"variable","name":"R","type":"string","initialValue":"(\\w+)\\s*?([<>!=]+)?\\s*?(\\d+|\\w+|[?<=\"].*?[?=\"]|[?<='].*?[?='])\\s*?","comment":"","isStatic":false,"isConstant":true},{"eventType":"variable","name":"operator","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"operand1","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"operand2","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"Result","type":"number","initialValue":"-1","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand1","value":"trim(RegexMatchAt(source, R, \"\", 1))"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operator","value":"trim(RegexMatchAt(source, R, \"\", 2))"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand2","value":"trim(RegexMatchAt(source, R, \"\", 3))"}}],"children":[{"eventType":"comment","text":"Number"},{"eventType":"block","conditions":[{"id":"compare-two-values","objectClass":"System","parameters":{"first-value":"str(float(operand1))","comparison":0,"second-value":"operand1"}},{"id":"compare-two-values","objectClass":"System","parameters":{"first-value":"str(float(operand2))","comparison":0,"second-value":"operand2"}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand1","value":"float(operand1)"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand2","value":"float(operand2)"}}],"children":[{"eventType":"block","conditions":[{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\">\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) > float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) < float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) = float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"==\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) = float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\">=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) >= float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) <= float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"!=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) <> float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<>\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"float(operand1) <> float(operand2)"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"-1"}}]}]},{"eventType":"comment","text":"String"},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"}],"actions":[],"children":[{"eventType":"block","conditions":[{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 = operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"==\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 = operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"!=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 <> operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<>\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 <> operand2"}}]}]}]},{"eventType":"block","conditions":[],"actions":[{"id":"set-function-return-value","objectClass":"Functions","parameters":{"value":"operand1 & \" \" & operator & \" \"& operand2 & \" // \" & (Result = 1 ? \"True\" : \"False\")"}}]}]}]}
    

    very simple to use.

    Functions.compare("5>=10")
    

    -----

    -----

    history:

    I'm designing a dialog system, that include a syntax for conditional branching.

    If it all is a string, they are all correct

    dictionary.Get("name") = "aaa"
    dictionary.Get("layout") != "Level"
    

    But once they are numbers, they are compared in dictionary order.

    "5" >= "10" // True. 5>1 (but is actually False)
    "5"<="40" // False. 5<=4 (but is actually True)
    

    How do I distinguish between numbers and strings in a string?

    I originally thought of the system condition 'Is value type', but it didn't work the way I thought it would, because if a string is passed in, it still be a string.

    Here is a quick sample:

    cdn.discordapp.com/attachments/253490735268102144/974243238829490246/ask_how_to_compare_using_string.c3p

  • Use the float(str) or int(str) system expressions to convert strings to numbers so you can compare them numerically.

  • If you know that these keys in the dictionary contain numbers, you need to convert them to integers before comparing.

    int(dictionary.Get("gold"))>=int(dictionary.Get("price"))
    

    If you want to check if a string value contains a valid number, you can add an event like this:

    If str(int(dictionary.Get("key"))) = dictionary.Get("key") : Set numVal to int(dictionary.Get("key")
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I tried to use dop2000's means to compare whether they are numbers or strings, it does work. It correctly distinguishes between numeric and string types

    But they still compare with the wrong result.

    Clipboard: (Please see the case above for the full code, Here are just added 2 conditions: System: str(float(operand1)) = operand1)

    {"is-c3-clipboard-data":true,"type":"events","items":[{"functionName":"Comparison","functionDescription":"","functionCategory":"","functionReturnType":"string","functionIsAsync":false,"functionParameters":[{"name":"source","type":"string","initialValue":"","comment":""}],"eventType":"function-block","conditions":[],"actions":[],"children":[{"eventType":"variable","name":"R","type":"string","initialValue":"(\\w+)\\s*?([<>!=]+)?\\s*?(\\d+|\\w+|[?<=\"].*?[?=\"]|[?<='].*?[?='])\\s*?","comment":"","isStatic":false,"isConstant":true},{"eventType":"variable","name":"operator","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"operand1","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"operand2","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"Result","type":"number","initialValue":"-1","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand1","value":"trim(RegexMatchAt(source, R, \"\", 1))"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operator","value":"trim(RegexMatchAt(source, R, \"\", 2))"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand2","value":"trim(RegexMatchAt(source, R, \"\", 3))"}}],"children":[{"eventType":"comment","text":"Number"},{"eventType":"block","conditions":[{"id":"compare-two-values","objectClass":"System","parameters":{"first-value":"str(float(operand1))","comparison":0,"second-value":"operand1"}},{"id":"compare-two-values","objectClass":"System","parameters":{"first-value":"str(float(operand2))","comparison":0,"second-value":"operand2"}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand1","value":"float(operand1)"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"operand2","value":"float(operand2)"}}],"children":[{"eventType":"block","conditions":[{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\">\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 > operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 < operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 = operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"==\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 = operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\">=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 >= operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 <= operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"!=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 <> operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"<>\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 <> operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"-1"}}]}]},{"eventType":"comment","text":"String"},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"}],"actions":[],"children":[{"eventType":"block","conditions":[{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"=\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 = operand2"}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"},{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"operator","comparison":0,"value":"\"==\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"Result","value":"operand1 = operand2"}}]}]}]},{"eventType":"block","conditions":[],"actions":[{"id":"set-function-return-value","objectClass":"Functions","parameters":{"value":"operand1 & \" \" & operator & \" \"& operand2 & \" // \" & (Result = 1 ? \"True\" : \"False\")"}}]}]}]}
    
  • Oh, I think I know what's going on. Because operand1 and operand2 are still variables of type String.

    I may have to use :

    Set Result to float(operand1) > float(operand2)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)