Version 1 of Text processing tips

Updated 2005-03-09 12:57:23 by CLN

Arjen Markus (9 March 2005) I created this page to collect small code fragments about the various tricks you can use to process text. Most of the time these tricks do not merit a page of their own, but they do merit a place on the Wiki :).


AM I had a problem with files that contain "continued lines". Here is a sketch:

    line with info \
          continued on the next line (see the backslash) \
          and the info is: Aha=BBBB
    another line with info - here the info is: Aha=CC

I needed to extract information from the complete lines. Now usually I read files line by line and analyse the lines one by one. You can not do that with this type of layout. Or can you? Here is my little trick:

   set contents [read $infile] 
   set contents [string map [list "\\\n" " "] $contents]
   foreach line [split $contents \n] {
      .. process the line ...
   }

This little fragment of code=:

  • reads the complete file (in my case they were not very large)
  • replaces the trailing backslash (and the newline) by a single space
  • splits the contents into separate lines again

No need to check if the line is complete or not - just use a few commands.


CLN When you get the contents of a text widget, you get an extra trailing newline. If you read the contents of a file, insert it in the widget, and just save those contents, you'll add a blank line at the end of the file for each save. The solution is to save one less character than [$text get 1.0 end] returns, something like [puts $fid string range [$text get 1.0 end] 0 end-1]].


... next trick? ...


[ Category Word and Text processing ]