Author Topic: How to validate a variable before converting?  (Read 1160 times)

Everburst

  • Newbie
  • *
  • Posts: 2
How to validate a variable before converting?
« on: March 13, 2023, 07:24:35 PM »
Is there a simple way to validate a text variable before converting into a boolean or integer? Page 45 of the documentation seems to indicate that I should be able to just convert it and the result should be "Not set" if it is not in a correct form. Instead, the command terminates with: "Error converting, [test_text] to [the_boolean] : Text parse error." Am I missing something basic?

It converts correctly if the form is valid, but I want to be able keep the program running with my own logic if not valid. I temporarily did an if statement to ensure the text was true or false before converting, but that is not a practical workaround for text to integer conversions. There has to be a better way than individually testing for the text version of every number combination before converting.


Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4761
  • RTFM
Re: How to validate a variable before converting?
« Reply #1 on: March 14, 2023, 08:40:56 AM »
You'll need to actually perform a conversion to check whether it's successful, however rather than using the "Convert a Variable Value" action, use the "Convert Text/Token" option of the relevant datatype's action (E.G. the "Set an Integer Value" action) and the "{TXT:}" token to retrieve the value of your source variable.
A failed conversion here will result in a log message, however the command will continue executing.


Page 45 of the latest documentation (this comes with the beta version) appears to be the start of the"‘Other Stuff’ Screens" section.
The documentation for the "'Convert a Variable Value'" action is on page 86, and states
Quote
Note - Any failed attempt to convert a value will result in the target variable having its value removed (Not set), and the command will immediately exit.

Everburst

  • Newbie
  • *
  • Posts: 2
Re: How to validate a variable before converting?
« Reply #2 on: March 14, 2023, 10:08:05 PM »
Thank you for the information. For future reference for people searching, the documentation I used with the stable version skipped "the command will immediately exit" part of that phrase. I also did not see that page 45 (page 48 on the beta documentation) refers to "converting" variables and their scope to pass to other commands. It was not in the context that I was thinking for converting between data types.

With that said, how exactly would I pass the token? When I pass the {TXT:convert_this_text} token into the "Another Boolean variable value" in the "Set a True/False (Boolean) Value" page, it always resolves to "Not set" regardless of what the value is and it does NOT send a log message. That also happens on the "Convert a variable" page, but that seems like a dead end based on your comments and the updated documentation.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4761
  • RTFM
Re: How to validate a variable before converting?
« Reply #3 on: March 15, 2023, 05:25:37 AM »
For Boolean values you'd need to use a condition to check the value, as you were already doing, which could then also assign the proper value to the target variable.

Numeric values can be converted as mentioned.


The "Another Boolean variable value" field is intended for passing the name of a Boolean variable, so that the value of that variable can be copied to the variable you're setting using the "Set a True/False (Boolean) Value" action.
You're passing in a token that retrieves the value of a text variable, which is then interpreted as the name of a Boolean variable. Unless a Boolean variable by that name happens to have been set, the value will logically be not set.