Re: Bests way to handle lines in files with only a return character?
- From: "Donal K. Fellows" <donal.k.fellows@xxxxxxxxxxxxxxxx>
- Date: Fri, 30 Dec 2005 23:01:14 GMT
slebetman@xxxxxxxxx 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. .
- References:
- Bests way to handle lines in files with only a return character?
- From: Keith
- Re: Bests way to handle lines in files with only a return character?
- From: slebetman@xxxxxxxxx
- Bests way to handle lines in files with only a return character?
- Prev by Date: Re: TclX on Windows - anybody successfully trap SIGTERM / SIGKILL
- Next by Date: Re: Underlining text on canvas
- Previous by thread: Re: Bests way to handle lines in files with only a return character?
- Next by thread: Getting GMT time
- Index(es):
Relevant Pages
|