Unable to Find Infinite Loop Cause

Clovertech Forums Read Only Archives Cloverleaf Tcl Library Unable to Find Infinite Loop Cause

  • Creator
    Topic
  • #55125
    Mohammed Iqbal
    Participant

      Hello everyone,

      I was hoping someone here could see something that I’m not seeing in my code which is producing an infinite loop?

      Code:

      set NTE $xlateInVals
      while {[string length $NTE] > 0} {
      set xlineLastSpaceIndex [string last x20 [string range $NTE 0 79]]
      set RLK [string range $NTE 0 $xlineLastSpaceIndex]
      regsub -all — $RLK $NTE “” NTE
      append RLK \.br\ $RLK
      }
      set xlateOutVals [list [string map {{ “” { “”} $RLK]]

      What I am trying to accomplish is, iterating through a long string and inserting line break codes, at the last white space within each set of the proceeding 80 characters.

      I plan to insert the code above as a Pre Proc within an xlate.

      If anyone could help me in tweaking the code so it works it would be greatly appreciated, I’ve been debugging for the past two days and am unable to see what I am missing.

      Thank you.

    Viewing 6 reply threads
    • Author
      Replies
      • #84206
        Keith McLeod
        Participant

          Won’t the value contained in the variable NTE always be greater than 0?

        • #84207
          Keith McLeod
          Participant

            I changed a few things.

          • #84208
            Mohammed Iqbal
            Participant

              Thank you for the response Keith. I attempted the code you suggested; however, the appending of the output values from the while loop only shows the output from the last iteration. So, the resulting output is whatever the output was from the last loop.

              Is there another way to do this? For example write the output values to a list, and later combine that list to a single string with injected code in the right places?

              Thank you for your help.

            • #84209
              Charlie Bursell
              Participant

                Off the top of my head I come up with the below.  A bit convoluted but should work.  What about if the string has no spaces?

                You may want to put a check for that at the top like:

                if {![regexp — {s} $NTE]} then do nothing – output same as input by default

                # Remember xlateInVals is a list

                set NTE [lindex $xlateInVals 0]

                # Init OUT string

                set OUT “”

                # Loop till empty

                while {$NTE ne “”} {

                   # Init tmp value

                   set tmp “”

                   # break into strings of 80 bytes

                   regexp — {^(.{0,80})(.*$)} $NTE {} tmp NTE

                   # Put longest part with space in part and the rest in rest

                   regexp — {^(.*s)(.*$)} $tmp {} part rest

                   # If spaces

                   if {$part ne “”} {

                       # Do not put .br. at start of string

                       if {$OUT ne “”} {append OUT \.br\}

                       # Append part of string to outpit

                       append OUT [string trim $part]

                       # Reset NTE

                       set NTE $rest$NTE

                   }

                   # If less than 80 bytes and no spaces jsut append the rest

                   if {[string length $NTE] < 80  &&

                           $NTE ne “” && ![regexp — {s} $NTE]} {

                       append OUT .\br.\$NTE

                       set NTE “”

                   }

                }

                # XlateOutVals is also a list

                set xlateOutVals

              • #84210
                Paul Bishop
                Participant

                  I have similar coding to break up OBX lines. What I use is the textutil package, specifically the adjust command.  The following command will take a long line and insert a newline (x0A) where ever it is needed.  In my example, $obx5 has already been set to the OBX-5 value

                  Code:


                  package require textutil
                  set max_length 73
                  set split_OBX5_list [split [textutil::adjust $obx5 -length $max_length] n]


                  Then you loop through $split_OBX5_list, doing what you need to it.  It will be a list of strings from your source that are 73 characters or less, breaking at a white space.  If you have a line that contains no white space, it will return just the one line.

                  Paul Bishop
                  Carle Foundation Hospital
                  Urbana, IL

                • #84211
                  Keith McLeod
                  Participant

                    Then you could add

                    set xlateOutVals

                      ]

                      The textutil option works very nice….

                    1. #84212
                      Mohammed Iqbal
                      Participant

                        Thank you Keith and Paul I sincerely appreciate your help. After some trial and error, the following code was able to accomplish what we were attempting to do:

                        Code:

                        set NTE [lindex $xlateInVals 0]
                        set RLKlist “”
                        while {[string length $NTE] > 1} {
                        set xlineLastSpaceIndex [string last x20 [string range $NTE 0 79]]
                        set RLK [string range $NTE 0 $xlineLastSpaceIndex]
                        regsub -all — $RLK $NTE “” NTE
                        lappend RLKlist $RLK
                        }
                        set RLK [join $RLKlist “\.br\”]
                        set xlateOutVals [list $RLK]


                        I may add the additional condition to consider situations for strings exceeding 80 characters missing spaces; however, given the field this script is intended to target, I would not expect for that to happen, as it is a free-text/notes field in the application. But it is better to be safe and account for that 1 off chance of the issue occurring, so I will attempt to add the additional condition and hope that it doesn’t mess up the script that’s already working.

                        Thanks again for your help, I really appreciate it.

                        Respectfully,

                    Viewing 6 reply threads
                    • The forum ‘Tcl Library’ is closed to new topics and replies.