TCL string manipulation help

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf TCL string manipulation help

  • Creator
    Topic
  • #49394
    Duy Nguyen
    Participant

      I have a string where I only want to look at characters after a specified character, let’s say a “hyphen”.

      for example, the original field contains a number string

    Viewing 11 reply threads
    • Author
      Replies
      • #61763
        Ryan Spires
        Participant

          Duy,

          If you are always dealing with a static value that separates the two parts of the string .i.e. the hyphen

          given ###-####  where you always want the 2nd set

          In the xlate you could  

          set value [lindex $xlateInVals 0]

          set value [split $value “-“]

          set xlateOutVals

            ]

            or

            set xlateOutVals

              “-“] 1]]

              Ryan

            1. #61764
              Duy Nguyen
              Participant

                But does anyone know the method to use within a TCL proc?

                I am wanting to KILL messages based on what’s after the “hyphen”  

                I’m not wanting to permanently modify that field..just want to LOOK at the portion of the STRING after the HYPHEN.

              • #61765

                Code:

                set string “459-7600”
                regexp {^.*-(.*)$} $string match newString
                puts $newString

                -- Max Drown (Infor)

              • #61766
                Duy Nguyen
                Participant

                  Max Drown wrote:

                  Code:

                  set string “459-7600”
                  regexp {^.*-(.*)$} $string match newString
                  puts $newString

                  Would this work?

                  ######################################################################

                  # Name: sched_div_filter

                  # Purpose: Kills all messages matching on scheduling department field of PV1-3 segment

                  #           where only messages that have scheduling department field of 7600 are passed

                  #           through and all other messages have a KILLING disposition.

                  # UPoC type: tps

                  # Args: tps keyedlist containing the following keys:

                  #       MODE    run mode (“start”, “run” or “time”)

                  #       MSGID   message handle

                  #       ARGS    user-supplied arguments:

                  #              

                  #

                  # Returns: tps disposition list:

                  #          

                  #

                  proc sched_div_filter { args } {

                     keylget args MODE mode               ;# Fetch mode

                     set dispList {} ;# Nothing to return

                     switch -exact — $mode {

                         start {

                             # Perform special init functions

                     # N.B.: there may or may not be a MSGID key in args

                         }

                         run {

                     # ‘run’ mode always has a MSGID; fetch and process it

                             keylget args MSGID mh

                    ## grabbing PV1 segment

                    set msg [msgget $mh]

                    set segmentList [split $msg r]

                    set segment [lindex [lregexp $segmentList ^PV1] 0]

                    set fieldSep [string index $msg 3]

                    set componentSep [string index $msg 4]

                  ##force componentSep to ^

                              set componentSep ^

                    set fieldList [split $segment $fieldSep]

                   ## zoning in on PV1-3

                    set field [lindex $fieldList 3]

                   ## zoning in on Scheduling Dept number delimited by “-” subfield

                      set subfieldList [split $field $componentSep]

                      set subField [lindex $subfieldList 0]

                             ## zoning in on the subfield after “hyphen” delimiter

                              set originalString $subField

                              regexp {^.*-(.*)$} $originalString match newString

                   echo =======================the value of “PV1-3” field is:  $subField

                   echo =======================the value of “Scheduling Dept #” subfield is: $newString

                   ## checking the sub-field and killing if condition is MET

                       if {[string equal $newString 7600]} {

                        lappend dispList “CONTINUE $mh”

                       } else {

                             lappend dispList “KILL $mh”

                         }

                  }

                         time {

                             # Timer-based processing

                     # N.B.: there may or may not be a MSGID key in args

                         }

                         

                         shutdown {

                     # Doing some clean-up work

                  }

                     }

                     return $dispList

                  }

                • #61767

                  Yes, but you may want to add some logic for unexpected situations.

                  For example …

                  Code:

                  if {regexp {^.*-(.*)$} $string match newString} {
                     # do something
                  }
                  else {
                     set newString “”
                     puts “ERROR!”
                  }

                  -- Max Drown (Infor)

                • #61768
                  Duy Nguyen
                  Participant

                    Max Drown wrote:

                    Yes, but you may want to add some logic for unexpected situations.

                    For example …

                    if {regexp {^.*-(.*)$} $string match newString} {
                    [code]if {regexp {^.*-(.*)$} $string match newString} {

                  • #61769

                    Well if you get a number with a slash or an empty string, the regexp will fail and newString will not get set. If your script then tries to access newString, which has not been set, then the script will “crash”. So, you need to code for that sort of thing.

                    -- Max Drown (Infor)

                  • #61770
                    Duy Nguyen
                    Participant

                      Max Drown wrote:

                      Well if you get a number with a slash or an empty string, the regexp will fail and newString will not get set. If your script then tries to access newString, which has not been set, then the script will “crash”. So, you need to code for that sort of thing.

                      So basically I need to write something that will bypass the part where it CHECKS if the CONDITION is MET if I’m not dealing with GOOD DATA?

                    • #61771
                      Duy Nguyen
                      Participant

                        Duy Nguyen wrote:

                        Max Drown wrote:

                        Well if you get a number with a slash or an empty string, the regexp will fail and newString will not get set. If your script then tries to access newString, which has not been set, then the script will “crash”. So, you need to code for that sort of thing.

                        So basically I need to write something that will bypass the part where it CHECKS if the CONDITION is MET if I’m not dealing with GOOD DATA?

                        nevermind, i think i know what you mean now…let me see and i’ll come back here.

                      • #61772

                        Yes.

                        regexp returns the number of matches found. So, “if {regexp {^.*-(.*)$} $string match newString} {” will alert you if a match was found or if a match was not found (the else block).

                        -- Max Drown (Infor)

                      • #61773
                        Duy Nguyen
                        Participant

                          Max Drown wrote:

                          Yes.

                          regexp returns the number of matches found. So, “if {regexp {^.*-(.*)$} $string match newString} {” will alert you if a match was found or if a match was not found (the else block).

                          Here is my proc with the additional checking logic… do you see any problem with this one?

                          ######################################################################

                          # Name: sched_div_filter

                          # Purpose: Kills all messages matching on scheduling department field of PV1-3 segment

                          #           where only messages that have scheduling department field of 7600 are passed

                          #           through and all other messages have a KILLING disposition.

                          # UPoC type: tps

                          # Args: tps keyedlist containing the following keys:

                          #       MODE    run mode (“start”, “run” or “time”)

                          #       MSGID   message handle

                          #       ARGS    user-supplied arguments:

                          #              

                          #

                          # Returns: tps disposition list:

                          #          

                          #

                          proc sched_div_filter { args } {

                             keylget args MODE mode               ;# Fetch mode

                             set dispList {} ;# Nothing to return

                             switch -exact — $mode {

                                 start {

                                     # Perform special init functions

                             # N.B.: there may or may not be a MSGID key in args

                                 }

                                 run {

                             # ‘run’ mode always has a MSGID; fetch and process it

                                     keylget args MSGID mh

                            ## grabbing PV1 segment

                            set msg [msgget $mh]

                            set segmentList [split $msg r]

                            set segment [lindex [lregexp $segmentList ^PV1] 0]

                            set fieldSep [string index $msg 3]

                            set componentSep [string index $msg 4]

                          ##force componentSep to ^

                                      set componentSep ^

                            set fieldList [split $segment $fieldSep]

                           ## zoning in on PV1-3

                            set field [lindex $fieldList 3]

                           ## zoning in on Scheduling Dept number delimited by “-” subfield

                              set subfieldList [split $field $componentSep]

                              set subField [lindex $subfieldList 0]

                                     

                                      ## zoning in on the subfield after “hyphen” delimiter

                                      set originalString $subField

                                     echo =======================the value of “PV1-3” field is:  $subField

                           

                                     #@@@@@@@@@@ checking for unexpected situations using IF statement @@@@@@@@@@

                                      if {regexp {^.*-(.*)$} $originalString match newString} {

                                    #~~~~~~~~~~ checking the sub-field and killing if condition is MET~~~~~~~~~~~

                                        ## “newString” takes on the modfified value

                                          regexp {^.*-(.*)$} $originalString match newString

                                     echo =======================the value of “Scheduling Dept #” subfield is: $newString

                                  if {[string equal $newString 7600]} {

                                                                      lappend dispList “CONTINUE $mh”

                           

                                                                               } else {

                                                                              lappend dispList “KILL $mh”

                                                                                       

                                                                                      }          

                                   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                                      } else {

                           

                                      lappend dispList “CONTINUE $mh”

                                             }

                          }

                                 time {

                                     # Timer-based processing

                             # N.B.: there may or may not be a MSGID key in args

                                 }

                                 

                                 shutdown {

                             # Doing some clean-up work

                          }

                             }

                             return $dispList

                          }

                        • #61774

                          Just test it out real well now. 🙂

                          -- Max Drown (Infor)

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