Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interation with WHILE

Status
Not open for further replies.

GRonken

Programmer
Oct 30, 2003
26
0
0
US
The following gives an extra line of "TEXT:"

set fidin [open C:/data/tcl/idxr.1 r]
set fidout [open C:/data/tcl/idxtest1.txt w]

while {![eof $fidin]} {

set inline [gets $fidin]
set text "TEXT:"
set Compare1 [string range $inline 0 7]
set Compare2 "RESULTS;"
set Compare3 [string range $inline 0 6]
set Compare4 "ENDTEXT"

if {[string compare $Compare1 $Compare2] == 0} {
set outline $inline
} elseif {[string compare $Compare3 $Compare4] == 0} {
set outline $inline
} else {
append text $inline
set outline $text
}
puts $fidout $outline
}
 
The loop doesn't check the condition until after it iterates. Therefore, you'll have to change the puts statement to:
if {!eof $fidin]} {puts $fidout $outline}

Bob Rashkin
rrashkin@csc.com
 
I tried it and still returns extra rows. (Even when I put a bracket after the ! in the if statement .... see I AM getting better <grin>

set fidin [open C:/data/tcl/idxr.1 r]
set fidout [open C:/data/tcl/idxtest3.txt w]

# need to make sure we trap the EOF; we'll use a while loop
while {! [eof $fidin]} {

set inline [gets $fidin]
set text &quot;TEXT:&quot;
set Compare1 [string range $inline 0 7]
set Compare2 &quot;RESULTS;&quot;
set Compare3 [string range $inline 0 6]
set Compare4 &quot;ENDTEXT&quot;

if {[string compare $Compare1 $Compare2] == 0} {
set outline $inline
} elseif {[string compare $Compare3 $Compare4] == 0} {
set outline $inline
} else {
append text $inline
set outline $text
}
if {![eof $fidin]} {puts $fidout $outline}
}
close $fidin
close $fidout
 
This is not an ideal way to do things of course.
A more concise approach might be something like
this:

Code:
proc openread {fname matches} {

   if {![catch {set fd [open $fname]} err_open]} {
        while {[gets $fd line] > -1} {
               foreach mat $matches {
                       if {[regexp $mat $line]} {puts $line}
                }
         }
         close $fd
    } else { 
         puts &quot;Could not open $fname: $err_open&quot;
    }
}

Where you are able to pass a list of regexps or
string compare exact matches to the function and
get the output you want without having to hard
code the strings you are looking for.

YMMV of course.

MMD
 
I have found the problem, the txt file I am bringing in has a control character at the end of each line. In Wordpad it looks like a little square, I assume it is a carriage return or something. I debugged it and watched it as it went through. If i remove it from the original text file, it works just fine. How do I trap for the character, especially since I am not sure what charcter it is? Suggestions anyone? Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top