Tcl...didnt get output

Status
Not open for further replies.

manchal

Newbie level 6
Joined
Feb 11, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,483
set xy [list a b c]
#set xy "{a b c {cd de {efg fgh}}} 1 2 {34 45 {567 678}}"
puts "$xy"
set l [ llength $xy ]
set j 0

puts "the index:"
#the proc of index
proc indx {x i {k ""}} {
set len [ llength $x ]
for {} {$i<$len} {incr i} {
set aa [lindex $x $i]
set b [llength $x]
if {($b == 1) && ($k == "")} {
return "[lindex $x $i ]-->$i $i"
} elseif {($b == 1)} {
return "[lindex $x $i ]-> $k- $i"
} else {
set s $i
if {$k != ""} {set s "$k-$s"}
return "[indx $aa $s]"
}
}
} ; #end of Proc

for {set m 0} {$m<$l} {incr m} {
set ak [indx $xy $j ] #updatng the valu evrytme proc is called.It updates the val only once.
#set j [lindex $ak 1]
puts "$j"
incr j
puts "$ak"
}



output:
a b c
the index:
0
a-->0 0
1

2
wanted as
0
a->0
1
b->1
2
c->2
 
Last edited:

Hai manchal,

The reason is the below for statement

for {} {$i<$len} {incr i} {

Its due to the value of i and len . First iteration the i value is 0 and len value is 1 so it enter into the loop . In second iteration ivalue is 1 and len value is also 1 so its not entering into it while recursive return statment. In third iteration ivalue is 2 and len value is 1 In this case also its not enterning. By analysing u will get it.

So while removing for we can easily achieve what u want.

change this statement return "[lindex $x $i ]-->$i $i" to statement return "[lindex $x 0 ]-->$i " to get the output which u want.
0
a->0
1
b->1
2
c->2

run the below code it will print the output which u need..

Code:
set xy [list a b c]
#set xy "{a b c {cd de {efg fgh}}} 1 2 {34 45 {567 678}}"
puts "$xy"
set l [ llength $xy ]
set j 0

puts "the index:"
#the proc of index
proc indx {x i {k ""}} {
set len [ llength $x ]
set aa [lindex $x $i]
set b [llength $x]
if {($b == 1) && ($k == "")} {
#return "[lindex $x $i ]-->$i $i"
return "[lindex $x 0 ]-->$i "
} elseif {($b == 1)} {
return "[lindex $x $i ]-> $k- $i"
} else {
set s $i
if {$k != ""} {set s "$k-$s"}
return "[indx $aa $s]"
}
} ; #end of Proc

for {set m 0} {$m<$l} {incr m} {
set ak [indx $xy $j ]
#set j [lindex $ak 1]
puts "$j"
incr j
puts "$ak"
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…