-2

I am trying to increment the output by 2 (every line increment by 2) while reading in a file.

Here is my code:

Input.txt:

green
yellow
grey
purple

SCRIPT:

set infile [open "input.txt" r]
set x1 0
set y1 0

set x2 100
set y2 100

while {[gets $infile line] >=0} {
    puts " $x1 [expr {$y1+2}[ [expr {$x2+0.124}] [expr {$y2+2} +0.124]"
}

Expected output:

green
0 2 0.124 2.124
yellow
0 4.0 0.124 4.124
grey 
0 6.0 0.124 6.124
purple 
0 8.0 0.124 8.124

1 Answer 1

1

Your code, as is, has syntax errors:

Error: extra characters after close-brace
        Use error_info for more info. (CMD-013)

Your for loop doesn't use the $line variable value, so how can you expect puts to print one of the colors from input.txt?

This code will do what you describe in your question (increment a value by 2 for each line in the file)

set infile [open "input.txt" r]

set x1 0

while {[gets $infile line] >=0} {
    puts $line
    puts $x1
    incr x1 2
    
}

this prints:

green
0
yellow
2
grey
4
purple
6
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.