Replace segment?

  • Creator
    Topic
  • #49636
    Rick Pritchett
    Participant

    # Want to replace the segment in the list with the new one created with the string map. Any ideas?

    proc String_Map { 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

           set mh [keylget args MSGID]

       set dispList

         set msg [msgget $mh]

         set fldSep [string index $msg 3]

         set subSep [string index $msg 4]

         set segList [split $msg r]

      set PV1_n [lindex [lregexp $segList {^PV1}] 0]

      set PID_n [lindex [lregexp $segList {^PID}] 0]

      set PV1_n [string map “& –” $PV1_n]

      set PID_n [string map “& –” $PID_n]

      echo “$PV1_n”

      # Want to replace the segment in the list with the new one created with the string map. Any ideas?

             }

             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

      }

Viewing 1 reply thread
  • Author
    Replies
    • #62810
      Charlie Bursell
      Participant

      The lregexp command will return a list of segments matching the regular expression which is fine if all you want to do is examine the segments.  However if you want to change something you must know it’s location in the list.  Try this:

      set pidLOc [lsearch -regexp $segList {^PID}]

      set pv1Loc [lsearch -regexp $segList {^PV1}]

      set PV1_n [lindex $segList $pv1Loc]

      set PID_n [lindex $segList $pisLoc]

      set PV1_n [string map “& –” $PV1_n]

      set PID_n [string map “& –” $PID_n]

      set segList [lreplace $segKist $pidLoc #pidLoc $PID_n]

      set segList [lreplace $segKist $pv1Loc #pv1Loc $PV1_n]

      # DO NOT FORGET THIS STEP!

      msgset $mh [join $segList r]

      FWIW, the lsearch command is better than lregexp if if you have Tcl 8.4 (Cloverleaf 5.4.1 or later)

    • #62811
      Rick Pritchett
      Participant

      thanks alot Charlie

      i had to correct a few errors but otherwise does the job here is the code for anyone else.  

                 set mh [keylget args MSGID]

         set dispList

           set msg [msgget $mh]

           set fldSep [string index $msg 3]

           set subSep [string index $msg 4]

           set segList [split $msg r]

           set PID_Loc [lsearch -regexp $segList {^PID}]

                   set PV1_Loc [lsearch -regexp $segList {^PV1}]

                   set PV1_n [lindex $segList $PV1_Loc]

                   set PID_n [lindex $segList $PID_Loc]

                  set PV1_n [string map “^ –” $PV1_n]

                  set PID_n [string map “& –” $PID_n]

                  set segList [lreplace $segList $PID_Loc $PID_Loc $PID_n]

                  set segList [lreplace $segList $PV1_Loc $PV1_Loc $PV1_n]

        # DO NOT FORGET THIS STEP!!!!!!!!!!!!!!!

                  msgset $mh [join $segList r]

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

Forum Statistics

Registered Users
5,119
Forums
28
Topics
9,293
Replies
34,435
Topic Tags
286
Empty Topic Tags
10