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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i write variables with a loop in the file

Status
Not open for further replies.

scilab

Programmer
May 10, 2011
3
DE
hi guys,
i'm making a graphic for my Scilabprogramm with TCl/Tk.And i am new in Tcl/TK. i want to write variables that from my graphic into a File.My codes are:

proc savefile {}{
global name
global jointnr
for { set i 1} { $i <= $jointnr } { incr i +1 } {
global theta$i
global alpha$i
global a$i
global s$i
global poor$i

}

set filename [tk_getSaveFile -filetypes $types -parent. -initialfile $name.txt ]
set fh [open $filename "w+"]

puts $fh "$name"
puts $fh "$jointnr"

puts $fh "$alpha1 $a1 $theta1 $s1 "
puts $fh "$alpha2 $a2 $theta2 $s2 "


close $fh

}

i get the variables "name","jointnr","alpha1","a1".... from outside. I mean i give the values on the graphic,then the codes get the values and save in the file "$name.txt"(it works already)

but my question: how should i do so that i can write the variabels with a loop into the File.
i try something like :
for {set i 1}{i<=$jointnr}{incr i+1}{
puts $fh "$alpha$i $theta$i $a$i $s$i"
}
but it dont work and i dont know where is wrong. Has someone any idea??
thanks a lot!!!
 
instead of puts $fh "$alpha$i $theta$i $a$i $s$i"
try this:
Code:
puts $fh "[set alpha$i] [set theta$i] [set a$i] [set s$i]"

_________________
Bob Rashkin
 
thank you for your reply.i have tried your codes, but it didn't work.Now i try to set the another parameters to "" with:
for { set i 1} { $i <= 6 } { incr i +1 } {
global theta$i
global alpha$i
global a$i
global s$i
global poor$i
if { $i > $jointnr}{
set theta$i ""
set alpha$i ""
...
}
set filename [tk_getSaveFile -filetypes $types -parent. -initialfile $name.txt ]
set fh [open $filename "w+"]

puts $fh "$name"
puts $fh "$jointnr"

puts $fh "$alpha1 $a1 $theta1 $s1 "
puts $fh "$alpha2 $a2 $theta2 $s2 "
puts $fh "$alpha3 $a3 $theta3 $s3 "
puts $fh "$alpha4 $a4 $theta4 $s4 "
puts $fh "$alpha5 $a5 $theta5 $s5 "
puts $fh "$alpha6 $a6 $theta6 $s6 "
close $fh

}

then can i save the same parameters as the number of joints into the file.It's not very efficient, but it works at least.
Thank you again for your tipps


 
scilab, if I am not mistaken, I think what you are looking for is:

set i 1; while {$i<=$jointnr} {eval "puts \$fh \"\$alpha$i \$theta$i \$a$i \$s$i \""; incr i}

Two things:
1. Maybe a typo, but you missed the dollar sign in your example:
for {set i 1}{$i<=$jointnr}{incr i+1}{

2. you loose a little on performance with 'eval', but if a few ms's is not a problem, I think your substitution problem is solved with this method.
 
thacoda, thanks for your answer. i have tried your codes. it works well. i have also finded a solution during the time. i wrote:
for { set i 1} { $i <= $jointnr } { incr i +1 } {

puts $fh "[set theta$i] [set s$i] [set a$i] [set alpha$i] [set poor$i]"
}
it works also. But thanks again for you tipps.

I have now an another trouble: i want to write some entries, let them get the values, then i want to divide the values by 180 and show it again with labels. My codes are:

for { set j 1} { $j <=$jointnr } { incr j +1 } {
entry .sci.gelenkvargww$j -textvariable gelenkvargww$j -width 10
grid .sci.gelenkvargww$j -row 8 -column $j -padx 20 -pady 10

set gw $gelenkvargww$j
set gw [expr {($pi)*(expr{[set gelenkvargww$j]})/180 }]
set gw [format "%.4f " $gw ]
label .sci.gelenkvargwwrad$j -text "$gw" -width 20
grid .sci.gelenkvargwwrad$j -row 10 -column $j -padx 20
}
it don't work with three "set "
i don't really know how can i change it.
have you any idea??

 
Code:
% set gelenkvargww1 42
42
% set j 1
1
% expr {[set gelenkvargww$j]/180.0}
0.233333333333
%

careful with your {} and []. Also if you want floating point division, you need to make sure that either the numerator or denominator is in fact a floating point number (or both).

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top