I have a TCL array , say arr. It has the following contents :
set arr(apple) "red"
set arr(grape) "green"
set arr(one) 1
set arr(2) "two"
set arr(0) "ZERO"
set arr(banana) "yellow"
set arr(three) 3
My aim is to push the element with index 0 to the last while maintaining the same order. I tried this solution
set arrList [array get arr]
foreach {key val} $arrList {
if {$key==0} then {
set a [lsearch $arrList $key]
set arrList [lreplace $arrList $a [incr a]]
lappend arrList $key $val
}
}
array set arr $arrList
My idea was to append the element and it's value (0 and "ZERO") and then convert it back again to an array. But then I realized that TCL array are implemented as Hash Table , so order can't be preserved.
So is there any way to solve this problem ? And if I have made any mistake anywhere , please do the required corrections. PS : I don't know if this is repeated.
Thanks