How to check if variable is defined or set?

AkashiDom

Active member
I want to make a macro but there's one problem.
I have this dictionary called TEMP.

As you might expected, in each run I'll delete it.
Then sometimes any key that need in that dictionary will be undefined and cause {lv=TEMP[undefined_key]} as output instead " ".

To fix this, I want to set default value if variable isn't set.
 

Dm114

Well-known member
I want to make a macro but there's one problem.
I have this dictionary called TEMP.

As you might expected, in each run I'll delete it.
Then sometimes any key that need in that dictionary will be undefined and cause {lv=TEMP[undefined_key]} as output instead " ".

To fix this, I want to set default value if variable isn't set.
As you noticed, when a variable doesn't exist, its name is returned instead of its value. It's an easy way to check that. When testing, just be careful that variable names can be surrounded by Curly { } or Square [ ] brackets, depending on MD settings.
 

AkashiDom

Active member
As you noticed, when a variable doesn't exist, its name is returned instead of its value. It's an easy way to check that. When testing, just be careful that variable names can be surrounded by Curly { } or Square [ ] brackets, depending on MD settings.
Thanks, I'll try that. Might work with regular expression?
Like
JavaScript:
/\{\l\v\=*\}/g

If that so, how do I make the if?
I know regular expression but only for replacing word.

Edit: I tried regular expression and it didn't work
Screenshot_20240203_235818.jpg
 
Last edited:

Dm114

Well-known member
Thanks, I'll try that. Might work with regular expression?
Like
JavaScript:
/\{\l\v\=*\}/g

If that so, how do I make the if?
I know regular expression but only for replacing word.
You can use Plain test (non Regex) with ?lv=var_name?
 

Dm114

Well-known member
Thanks
Btw, did you mean
Code:
?lv=*?
No. As * stands for any number of characters, ? after * is useless.

A Regex rule maybe a bit difficult to write if your variable names contain special characters such as dot, square brackets...
 

Endercraft

Moderator (& bug finder :D)
Actually it's pretty simple to know if it hasn't evaluated correctly, simply remove the last character of your result and compare.
See for yourself by changing the key variable in the attached macro.
Check_if_var_evaluated.png
 

Attachments

  • Check_if_var_evaluated.macro
    6.5 KB · Views: 1
Top