Re: Bests way to handle lines in files with only a return character?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Bests way to handle lines in files with only a return character?

From: Donal K. Fellows <donal.k.fellows@manchester.ac.uk>
Date: Sat Dec 31 2005 - 00:01:14 CET

slebetman@yahoo.com wrote:
> This way you also handle lines containing spacebars and tabs. It's also
> easy to handle coments in the file in a similar fashion:
> set commentChar "//"
> set n [string first $commentChar $line]
> if {$n != -1} {
> set line [string range $line $n end]
> }
> set line [string trim $line]

Hmm, as someone who likes REs I'd be tempted to replace those lines
above with this:

   set line [string trim [regsub {//.*} $line {}]]

But then there's the whole issue of quoting (what if you want to retain
the comment character?) It's usually easier to only allow comments to be
"whole line" comments, resulting in cleanup code like this:

   set line [regsub {^//.*} [string trim $line] {}]

Though if you're processing lines in a loop, it's actually more common
to do this:

   foreach line $theData {
      set line [string trim $line]
      if {[string match //* $line]} continue ;# No comment lines
      if {$line eq ""} continue ;# No blank lines
      # Process line here...
   }

There are probably as many variations on this as there are Tclers. :-)

Donal.
Received on Tue Jan 3 03:09:53 2006