Replacing the nth Occurrence of a Character in a String

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Replacing the nth Occurrence of a Character in a String

Viewing 7 reply threads
  • Author
    Replies
    • #70081
      Jim Kosloskey
      Participant

      Take a look at the string replace Tcl command.

      sting replace $some_string 6 6 “~”  

      Might work for you.

      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

    • #70082
      David Harrison
      Participant

      Jim,

      That just replaces the 6th character, not the 6th occurrence of a character.

      This works but it’s a bit long winded. I wondered if there was a more concise method.

      Code:


      set imsg {10930,1234567890,123456,PATIENT,FNAME,1011945,TST1,Result 1,TST2,Result 2}
      set omsg [split $imsg “,”]
      set omsg [linsert $omsg 6 ~]
      set omsg [join $omsg ,]
      set omsg [string map -nocase {,~, ~} $omsg]
      puts $omsg

      Thanks,

      Dave

    • #70083
      Jim Kosloskey
      Participant

      Sorry – I misunderstood what you wanted to accomplish.

      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

    • #70084
      Jim Kosloskey
      Participant

      Does the inbound message have to have the tilde before Xlate or is it the outbound (from Xlate) message that needs the tilde?

      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

    • #70085

      Try this:

      Code:

      set var “10930,1234567890,123456,PATIENT,FNAME,1011945,TST1,Result 1,TST2,Result 2”
      set res “10930,1234567890,123456,PATIENT,FNAME,1011945~TST1,Result 1,TST2,Result 2”

      regsub — {(.*?,.*?,.*?,.*?,.*?,.*?),(.*?,.*?,.*?,.*?)} $var {1-2} newVar
      puts $newVar
      #returns: 10930,1234567890,123456,PATIENT,FNAME,1011945-TST1,Result 1,TST2,Result 2

      -- Max Drown (Infor)

    • #70086
      David Harrison
      Participant

      Jim,

      This is for pre-Xlate. The tilde is the terminator for the first VRL in my HRL.

      Max’s code works – thanks Max

    • #70087
      Tom Rioux
      Participant

      Just thought I’d put out another way to skin the cat:

      set var “10930,1234567890,123456,PATIENT,FNAME,1011945,TST1,Result 1,TST2,Result 2”

      set ridx [lindex [regexp -all -indices -inline , $var] 5]

      set varout [string replace $var [lindex $ridx 0] [lindex $ridx 1] ~]

      RETURNS:

      10930,1234567890,123456,PATIENT,FNAME,1011945~TST1,Result 1,TST2,Result 2

      Hope this helps…

      Tom Rioux

    • #70088
      David Harrison
      Participant

      Tom,

      That’s more elegant a solution than I could come up with, plus I can re-use the code for other manipulation I need to do.

      Many thanks,

      Dave

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

Forum Statistics

Registered Users
5,126
Forums
28
Topics
9,296
Replies
34,439
Topic Tags
287
Empty Topic Tags
10