Adding comma to list of numbers

Homepage Clovertech Forums Read Only Archives Cloverleaf Tcl Library Adding comma to list of numbers

  • Creator
    Topic
  • #50726
    Gary Atkinson
    Participant

    I have a long flat file with a list of numbers.  I want to add a comma in front of each number.  So, my file looks like this:

    1234

    5678

    8908

    And I want it to look like:

    ,1234

    ,5678

    ,8908

    What is the best approach to do this in tcl?

Viewing 5 reply threads
  • Author
    Replies
    • #67265
      Charlie Bursell
      Participant

      If all the numbers are part of the same record, I would do something like:

              regsub -all — {m(d+?)M}  $rec {,1} rec

    • #67266
      Gary Atkinson
      Participant

      How do I add in that regsub into reading in my flat file?

    • #67267
      Charlie Bursell
      Participant

      You asked how to do this in Tcl.  I assumed, depending on where and how you are processing the flat file, you would know how to access the message.

      If you put a tcl script in your inbound thread you would simply add this as part of the script.  To be more specific I would have to know how you are reading the flat file.  Are you reading a record at a time or is it coming in as a complete file? Do you then forward through the engine as a single file or as multiple records?

    • #67268
      Gary Atkinson
      Participant

      Sorry I am not doing this in the engine

    • #67269
      Charlie Bursell
      Participant

      if this is a file of records with each erecord containing a number do this:

      set inp [read_file -nonewline file]

      regsub -line -all — {^(d+)} $inp {,1} out

      write_file newfile $out

      As I said, it is hard to give an answer unless we know specifically what you are dealing with.

      You should have enough to figure it out if there are variances to my assumptions

    • #67270
      Gary Atkinson
      Participant

      Thanks, I got it to work.

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

Forum Statistics

Registered Users
5,127
Forums
28
Topics
9,300
Replies
34,445
Topic Tags
288
Empty Topic Tags
10