10 Replies Latest reply on Nov 10, 2007 8:26 AM by 3Dgregory-l6Pj6i

    string(someChar) returns "" when string(a) used

    3Dgregory-l6Pj6i Level 1
      It appears that string(a) is returning a value of Empty or “”, when I expected it to rreturn a value of “a”. As a result when a type a for someChar it is returning true:

      on isDigit someChar
      if “0123456789” contains string(someChar) then
      return TRUE
      else
      return FALSE
      end if
      end
        • 1. Re: string(someChar) returns "" when string(a) used
          robdillon Most Valuable Participant
          The way you've written that function, you will get back only true or false. If you want to have the function return the actual string, then you'll have to change the code. If you want to get the character back if the result is true then maybe something like this will work for you:

          on isDigit someChar
          if "0123456789" contains string(someChar) then
          return someChar
          end if
          return false
          end

          or the alternate:


          on isDigit someChar
          if "0123456789" contains string(someChar) then
          return true
          end if
          return someChar
          end

          if you want the value of someChar if the that character isn't in the string used for comparison.
          • 2. Re: string(someChar) returns "" when string(a) used
            3Dgregory-l6Pj6i Level 1
            I tried using the following as you suggested Rob:

            on isDigit someChar
            if "0123456789" contains string(someChar) then
            return someChar
            end if
            return false
            end

            I only get the result of 0 when I enter a as "a":
            put isDigit (1)
            -- 1
            put isDigit (a)
            -- <Void>
            put isDigit ("a")
            -- 0

            I get void when I enter an argument of a, even though the parameter should be casted as a string by using string(someChar) inside the handler.
            • 3. Re: string(someChar) returns &quot;&quot; when string(a) used
              Level 7
              3Dgregory wrote:
              > I tried using the following as you suggested Rob:
              >
              > on isDigit someChar
              > if "0123456789" contains string(someChar) then
              > return someChar
              > end if
              > return false
              > end
              >
              > I only get the result of 0 when I enter a as "a":
              > put isDigit (1)
              > -- 1
              > put isDigit (a)
              > -- <Void>
              > put isDigit ("a")
              > -- 0
              >
              > I get void when I enter an argument of a, even though the parameter should be
              > casted as a string by using string(someChar) inside the handler.
              >
              >

              Well if you type put isDigit(a) in the message window, a is treated as a
              variable who's value has not been set (ie void).

              so put isDigit (a)
              -- <Void>

              is the same as

              put isDigit (void)
              -- <Void>

              The odd thing is this:

              put "0123456789" contains string(void)
              -- 1

              or put another way

              put "0123456789" contains ""
              -- 1

              I guess every string contains void? Hmmm

              Anyway since the expression evaluates as true, the parameter passed to
              the function is returned, thus you get <void> back. You probably should
              add code that checks to make sure the value passed to the function is
              not void. If it is, have the function return whatever is appropriate.
              • 4. Re: string(someChar) returns &quot;&quot; when string(a) used
                Level 7
                Try this one.

                on isDigit someChar
                case TRUE of
                (someChar = void) : return False
                ("0123456789" contains string(someChar)) : return TRUE
                otherwise : return False
                end case
                end



                If you say put isDigit(a) without setting a to some value you will get
                false back (instead of void as before). But try this in the message window.

                a = 1 (then press ENTER)
                put isDigit(a)
                -- 1\

                which is as expected. By the way I did alter the function slightly to
                either return TRUE or FALSE which is more what one would expect a
                function named isDigit to do. If you did actually need it to return the
                input character instead of true, you can of course change it back.
                • 5. string(someChar) returns &quot;&quot; when string(a) used
                  McFazstp Level 1
                  You could use

                  on getDigit someChar
                  if isDigit( someChar ) then
                  return someChar
                  else
                  return EMPTY
                  end if
                  end getDigit

                  on getAlpha someChar
                  if NOT isDigit( someChar ) then
                  return someChar
                  else
                  return EMPTY
                  end if
                  end getAlpha

                  on isDigit someChar
                  return NOT voidP( value( someChar ) )
                  end isDigit
                  • 7. Re: string(someChar) returns &quot;&quot; when string(a) used
                    3Dgregory-l6Pj6i Level 1
                    The idea that a is returning <void> because it is an undeclared variable is the correct reason why <void> was being returned. I thought that too today while reading through some of "Lingo in a Nutshell".

                    Also checking for the case of where (someChar = void) was correct to do because it allows for the fact that the undeclared variable <void> does return as true. So what I got to get the correct values, as Dave C suggested was return someChar.

                    I used if..then statements instead of cases as follows:
                    on isDigit someChar
                    if someChar = void then
                    return false
                    else if "0123456789" contains string(someChar) then
                    return someChar
                    else
                    return false
                    end if
                    end

                    Then I got expected results when I called the handler from the Message window:
                    put isDigit (1)
                    -- 1
                    put isDigit (a)
                    -- 0
                    put isDigit (2)
                    -- 2
                    • 8. Re: string(someChar) returns &quot;&quot; when string(a) used
                      3Dgregory-l6Pj6i Level 1
                      Wow, that is pretty eloquent. Using the predicate voidP in the statement:
                      return NOT voidP(value(someChar)) does say it all in one line, and you get the correct values from the Message window.
                      Excellent suggestion, McFazstp.
                      • 9. Re: string(someChar) returns &quot;&quot; when string(a) used
                        Level 7
                        But isn't there a problem with having the function return someChar when
                        the input does consist of only numbers and false when it contains
                        letter? isDigit(0) will return 0, just like any string or VOID. Maybe it
                        doesn't matter for your particular case but how will your code know when
                        0 means the input was 0 or contained some letters?

                        You might also consider that it means the return value can be a string
                        or an integer.

                        put isDigit("1")
                        -- "1"

                        put isDigit(1)
                        -- 1

                        Only you really know how your program needs to behave, but it seems to
                        me that isDigit should only return true or false. And for that the code
                        suggested by McFazstp seems to be the most efficient.
                        • 10. Re: string(someChar) returns &quot;&quot; when string(a) used
                          3Dgregory-l6Pj6i Level 1
                          You are correct, Dave. The purpose of a function like isDigit is to check to see if the argument passed is a digit. Therefore, the return should be true or false and not someChar.

                          Since all we need to do is check to see eliminate the possibility of entering a letter that is an undeclared variable as an argument, McFazstps handler would work without casting a value because the letters are void and the value of the number is not needed.

                          As a result, this would also work:

                          on isDigit someChar
                          return NOT voidP( someChar )
                          end isDigit

                          instead of

                          on isDigit someChar
                          return NOT voidP( value(someChar ) )
                          end isDigit