0
#!/bin/sh
# This is a trial program

puts "++++++++++++++++++++++++++++++++++++++++++++++++++"
set y "0.0.0.0"

set z [split $y "."]
puts "z=$z"
lreplace $z 0 5
puts "z $z"
set v [llength $z]
puts "length of array=  $v"
puts "in the loop-------->\n"
puts " "
incr v -1
puts $v
for {set ml $v } { $ml >= 0} { puts "$ml =ml"} {
    for { set nl [lindex $z $ml]} { $nl >=4} { puts "$nl=nl"} {
        puts $nl
        after 2000
        lset z  $ml $nl
        incr $nl
    }
    after 2000
    incr ml -1
}               

I am not able to enter the second for loop, is this a formatting issue ? gives me some weird error. I added the sleep just to check whats happening so ignore that.

4
  • 1
    Is there any reason you're using lreplace $z 0 5? Commented Jan 19, 2014 at 9:40
  • +1 Jerry's comment: lreplace $z 0 5 doesn't actually do anything in this program. The command creates a result list based on the elements in z with all those elements up to (non-existent) index 5 deleted, and then you don't assign this result list anywhere. Commented Jan 19, 2014 at 11:10
  • If your intention was to replace the 0:s with 5:s, this will work better: set z [lmap elem $z { expr { $elem == 0 ? 5 : $elem } }] Commented Jan 19, 2014 at 11:22
  • should be incr nl not incr $nl Commented Jan 19, 2014 at 17:20

2 Answers 2

2

In your code your inner loop is only evaluating if nl >=4. nl will be initialized as 0 from [lindex $z $ml]

Since you are incrementing $nl, my guess is you should change this line:

for { set nl [lindex $z $ml]} { $nl >=4} { puts "$nl=nl"} {

to this instead:

for { set nl [lindex $z $ml]} { $nl <=4} { puts "$nl=nl"} {
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Niall. I tried that and nl never increments. 0=nl 0=nl 0=nl 0=nl ^C
In fact the 0th element in z is also not getting replaced :(
z=0 0 0 0 z 0 0 0 0 length of array= 4 in the loop-------->
Your incr statement seems a bit odd $nl is a value, you may need to use incr nl
Absolutely right ! Just figured it out. Thank you very much for your inputs
2

Was it perchance something like this you intended?

# This is a trial program

puts "++++++++++++++++++++++++++++++++++++++++++++++++++"
set y "0.0.0.0"

set z [split $y "."]
puts "\$z=$z"
set v [llength $z]
# the term 'array' means associative array in Tcl, better use 'list'
puts "length of list=  $v"
puts "in the loop-------->\n\n"
incr v -1
puts "\$v=$v"
for {set ml $v} {$ml >= 0} {incr ml -1} {
    for {set nl [lindex $z $ml]} {$nl <= 4} {incr nl} {
        lset z $ml $nl
        puts $z
    }
}

Note that I've moved the incr command invocations to the third argument (the next command string, as the documentation puts it) of the for command invocations. You can put anything you want to run at the end of each iteration there, including puts commands as you did, but it's a convention and good practice to have the loop-control-changing commands (whatever they may be) there, and not much else.

2 Comments

You'll want to change your #! line.
Yes, well, I just copied it from the OP's code. I figured if it worked for him I'd just leave it in. But now I think I'll remove it to avoid confusion. Thank you for pointing it out.

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.