Tcl Question

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Tcl Question

  • Creator
    Topic
  • #50713
    David Harrison
    Participant

      In tcl, how can I check that a variable contains all spaces

    Viewing 7 reply threads
    • Author
      Replies
      • #67214
        Tom Rioux
        Participant

          Hi David,

          The following should get you what you want:

          [regexp \s+ $a]

          Hope this helps…

          Tom Rioux

        • #67215
          Anonymous
          Participant

            Are you wanting to know if it only has spaces ?

            Or if it has any spaces ?

          • #67216
            David Harrison
            Participant

              I want to know if its only spaces.

              Tom’s code works if there are any spaces but also other characters

            • #67217
              Anonymous
              Participant

                I’m sure there are a dozen ways to do this.

                But how about this.

                string length [string trim $VAR ” “]

                If it is zero then it had all blanks or was null

                If it has any value then you had non-spaces.

                I’m sure Charlie is watching so I’m waiting on his comments on how to improve this.  

              • #67218
                David Harrison
                Participant

                  Yes. That’s what I eventually came up with – and it works.

                • #67219
                  Tom Rioux
                  Participant

                    If you want just a string of spaces, with no characters, then this should do the trick for you:

                    regexp “^[ ]+$” $a

                    Example:

                    hcitcl>set a ”     “

                    hcitcl>set b ”   abc”

                     abc

                    hcitcl>regexp “^[ ]+$” $a

                    1

                    hcitcl>regexp “^[ ]+$” $b

                    0

                    Hope this helps.   Thanks John for pointing out the error of my ways.  Even when I worked for you, you were (and still are) so good at doing that.  I think that makes your day!!!  LOL 😀

                  • #67220
                    David Harrison
                    Participant

                      That’s the one.

                      Thanks chaps,

                      Dave

                    • #67221
                      Charlie Bursell
                      Participant

                        If you wanted to know if the string consisted of 0 r more spaces simply:

                        if {[lempty $var]]} { var is empty or only contains spaces }

                        If you want to make sure it has at least 1 space but nothing else

                        if {[string length $var] > 0 && ![regexp — {S} $var]} { var is not empty and contains only spaces}

                    Viewing 7 reply threads
                    • The forum ‘Cloverleaf’ is closed to new topics and replies.