String Replace?

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf String Replace?

  • Creator
    Topic
  • #49401
    Rick Pritchett
    Participant

      im trying to use the string replace command but it seems to do nothing am i using it wrong?

      lassign $xlateInVals OBX5

      echo “hello”

      if { [string length $OBX5] > 80} {

      set Pos2 [string last ” ” $OBX5 79]

      string replace $OBX5 $Pos2 $Pos2 “~”

      } else {

       

      }

      set xlateOutVals

    Viewing 4 reply threads
    • Author
      Replies
      • #61802
        John Hamilton
        Participant

          No that is correct.

          I think you need to add

          set xlateOutvals [string replace ….]

        • #61803
          Tom Rioux
          Participant

            The problem appears to be with your “string last” command.  Unless it is merely a typo, you called it with too many args, resulting in an incorrect value.  See the example below:

            set a “Thomas Gregory Rioux”

            echo $a

            Thomas Gregory Rioux

            set Pos2 [string last ” ” $a 9]

            echo $Pos2

            6

            set Pos21 [string last ” ” $a]

            echo $Pos21

            14

            Try correcting your “string last” command and you should get the correct results.  Let me know if this helps.

            Tom Rioux

            Baylor Health Care Systems

            thomasri@BaylorHealth.edu

          • #61804
            Rick Pritchett
            Participant

              I found this on the TCL man page.

              string last string1 string2 ?lastIndex?

              Search string2 for a sequence of characters that exactly match the characters in string1. If found, return the index of the first character in the last such match within string2. If there is no match, then return -1. If lastIndex is specified (in any of the forms accepted by the index method), then only the characters in string2 at or before the specified lastIndex will be considered by the search. For example,

              string last a 0a23456789abcdef 15

              will return 10, but

              string last a 0a23456789abcdef 9

              will return 1.

            • #61805
              Greg Eriksen
              Participant

                I think the problem might be that in your string replace, you are just executing the command, you are not setting the value of OBX5 to the result of executing the string replace command. See the following:

                hcitcl>set OBX5 “0a23456789abcdef”

                hcitcl>echo $OBX5

                0a23456789abcdef

                hcitcl>set POS2 [string last a $OBX5 9]

                hcitcl>echo $POS2

                1

                hcitcl>set OBX5 [string replace $OBX5 $POS2 $POS2 “~”]

                hcitcl>echo $OBX5

                0~23456789abcdef

              • #61806
                Greg Eriksen
                Participant

                  I didn’t pick up on it before, but I think my suggestion is exactly what John Hamilton was pointing out in his earlier post.

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