Moving minus sign to front of decimal number

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Moving minus sign to front of decimal number

  • Creator
    Topic
  • #50277
    Robert Kersemakers
    Participant

    Hi all,

    We are receiving XML with decimal fields, where the minus sign is at the end of the field:

    1234.5678-

    I want to use a regsub to change all occurences into

    -1234.5678

    I already have one that works:

    regsub -all — “(>)(.*)(-)(<)" $msg "\1\3\2\4" msg But this also works for character fields ending with a minus sign, like: test-

    So I would like to have a check that there is a decimal involved. I have tried a lot, but I can’t get it to work. Regular expressions always outsmart me…

    Anyone have a clue?

    Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

Viewing 7 reply threads
  • Author
    Replies
    • #65438

      Use an if statement with regexp.

      Example:

      if {[regexp {.} $msg]} {
      [code]if {[regexp {.} $msg]} {

      -- Max Drown (Infor)

    • #65439
      John Mercogliano
      Participant

      Robert,

       Give this a try:

      regsub -all — {(>)(d*)([.])(d*)([-])(<)} $msg {152346} msg

      John Mercogliano
      Sentara Healthcare
      Hampton Roads, VA

    • #65440
      Charlie Bursell
      Participant

      Some real complicated regular expressions for an easy job  ðŸ˜‰

      set num 1234.5678-

      regsub — {(.*?)(-$)} $num {21} num

      echo $num

       ==> -1234.5678

      If the minus not at the end, it changes nothing

    • #65441
      Tom Rioux
      Participant

      If you wanted only numbers, then modify the code to be:

      regsub — {(.*?[0-9])(-$)} $num {21} num

      Have a great Monday!

      Tom Rioux

      Baylor Health Care

    • #65442
      John Mercogliano
      Participant

      Hi Charlie,

       In this case I think more complex is going to work better based on my understanding of his problem.  Robert, chime in and let me know if I’m completely off base.

      He appears to be looking to transform a whole xml msg at once not just and individual item, so I’m expecting his msg will look something like this:

      Code:


      123.345-
      123.456
      test.-
      123.6781-


      With an expected output like this:

      Code:


      -123.345
      123.456
      test.-
      -123.6781

      The dollar sign after the minus finds not matches in this case.

      John Mercogliano
      Sentara Healthcare
      Hampton Roads, VA

    • #65443
      Charlie Bursell
      Participant

      In that case:

      Assuming many of the same type

      foreach tag [regexp -all -inline — {[d.]+-} $xmlMsg] {

         set num “”

         regexp — {(.*?)} $tag {} num

         regsub — {(.*?)(-$)} $num {21} newnum

         set newtag [string map

        $tag]

           set xmlMsg [string map

        $xmlMsg]

        }              

        Of course it would be better if we knew more about what we were doing  ðŸ˜³

    • #65444
      Robert Kersemakers
      Participant

      Hi folks,

      Let me try to clarify. We are processing SAP IDocs in XML-format. If you have ever seen some of these: they are really a bunch of segments, which consist of fields. Something like:

      Code:


       
         12.254-
         MBGMCR02
         …
       

       
         20080909120000
         
         …
       

      So now we had the problem that SAP put the minus sign at the end of the decimal, like the KRATE field. We need to truncate some numeric fields to only 2 decimals, and the minus sign wrecked havoc with our solution for this:

      Code:

      set xlateOutVals [list [format “%.2f” $xlateInVals] ]


      This works with the minus sign at the front of the decimal, and I believe that the XML-standard requires the minus sign to be at the front.

      If we wanted decent output from SAP, then our SAP-guys had to work over every single numerical field with a possible negative outcome. Too much work. So I wanted to do this with one regsub.

      John’s first suggestion was a good example for me. I was close, but I guess escaping the decimal sign was where I went wrong: I tried it with ‘ .’ . Only problem with his solution was that negative integers aren’t processed. So I changed it into

      Code:

      regsub -all — {(>)(d*)([.]d*)*([-])(<)} $a {14235} b


      which seems to do the trick for me.

      Thanks for all your help, guys! Much appreciated.

      Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

    • #65445
      Charlie Bursell
      Participant

      Now that we know what you are doing  ðŸ™‚

      regsub -all — {>([d.]+?)-<} $xmlMsg {>-1<} xmlMsg

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

Forum Statistics

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