Question
· Dec 13, 2024

Condition for checking for non-empty string.

Hi,

My variable `check1` is a string.  It is either the empty string (for invalid/false answer) or a non-empty string for a valid/true input.  If it is valid, I want to return it.  I wrote this code:

    ret:$l(check1) check1

I do not like it once because it repeats the variable, but the main reason I do not like it is that if I do not insert the check $length(string) it will return false even for non-empty strings (due to conversion to integers for string prefixes).

I would like to ask you whether is there some nicer form in mumps to check for the non-empty strings than checking their length.

Thank you.

Product version: IRIS 2024.3
$ZV: IRIS for Windows (x86-64) 2024.1 (Build 267_2) Tue Apr 30 2024 16:37:07 EDT
Discussion (3)1
Log in or sign up to continue

Is return supposed to return a boolean (0/1) or the non empty value of check1?

I'm asking because you write "will return false even for non-empty strings (due to conversion to integers for string prefixes)".

In general to check for not empty string (check1'="") is used.

If you need to return a boolean depending of empty/not empty, then you can use:

return (check1'="") 

I have a large list of functions that returns strings.  I consider a string as true if it is non-empty.  Empty string means false for me.  I keep calling these functions until I get a true (non-empty string).  If none returns true, the result fails.  It is an implementation of a unification algorithm actually.

I did not know how to write the code more readable, this is why I was asking how to check for empty string in a nicer way.

I am not sure exactly what you want to do.  Is check1 just an ordinary string, or might it sometimes be a string that starts with a number (like a %Status variable)?

The unary plus operator (+) turns a string operand into a number.  If the string starts with the syntax of a number then +string returns that number and ignores characters in the string following the numeric syntax.  If the string does not start with a number (this includes the empty string) then +string returns 0.  It is possible for +string to signal <MAXNUMBER> if the characters at the beginning of string are too large for conversion to the computational numbers supported by ObjectScript.

You could try
  ret:+check1 check1
which would return check1 only if it were a non-empty string that starts with a non-zero number.

If you want to return a string value that could be tested as a boolean that you have to append "1" to the front of the string if +string=0 is true.