Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Save Procedure help

Posted by Avarice (Avarice), 15 July 2003
Code:
proc savecommand {} {
global int_nfield int_ipol int_wvln int_mag int_na int_sigma int_df int_ast
global int_coma int_ihighna int_theoff int_phioff int_deffs int_nrect int_nlayer
global int_3aref(1) int_3kref(1) int_nout int_dthick int_nrefine int_cmax int_cstep
global int_lensindex int_dndt int_normtemp int_ray int_raystep int_D1 int_aref([expr $int_nlayer + 2])
global int_kref([expr $int_nlayer + 2]) foo nrect nlayer
for {set i 1} {$i <= $int_nrect} {incr i 1} {
global int_wx($i)
global int_wy($i)
global int_xc($i)
global int_yc($i)
global int_phase1($i)
}
for {set j 1} {$j <= $int_nlayer}  {incr j 1} {
global int_2aref($j)
global int_2kref($j)
global int_2dref($j)
}
set file [open /export/home/aritchie/tclfiles/testwish.in w]
puts $file $int_D1 nonewline
puts $file $foo nonewline
puts $file $int_nfield nonewline
puts $file $foo nonewline
puts $file $int_ipol
puts $file $foo nonewline
puts $file $int_wvln nonewline
puts $file $foo nonewline
puts $file $int_mag nonewline
puts $file $foo nonewline
puts $file $int_na nonewline
puts $file $foo nonewline
puts $file $int_sigma
puts $file $foo nonewline
puts $file $int_df nonewline
puts $file $foo nonewline
puts $file $int_ast nonewline
puts $file $foo nonewline
puts $file $int_coma nonewline
puts $file $foo nonewline
puts $file $int_ihighna
puts $file $foo nonewline
puts $file $int_theoff nonewline
puts $file $foo nonewline
puts $file $int_phioff nonewline
puts $file $foo nonewline
puts $file $int_deffs
puts $file $int_nrect
for {set i 1} {$i <= $int_nrect} {incr i 1} {
puts $file $foo
puts $file $int_wx($i) nonewline
puts $file $foo nonewline
puts $file $int_wy($i) nonewline
puts $file $foo nonewline
puts $file $int_xc($i) nonewline
puts $file $foo nonewline
puts $file $int_yc($i) nonewline
puts $file $foo nonewline
puts $file $int_phase1($i) nonewline
}
puts $file $int_nlayer
puts $file $foo
puts $file $int_aref([expr $int_nlayer + 2]) nonewline
puts $file $foo nonewline
puts $file $int_kref([expr $int_nlayer + 2]) nonewline
for {set j 1} {$j <= $int_nlayer} {incr i 1} {
puts $file $foo
puts $file $int_2aref($j) nonewline
puts $file $foo nonewline
puts $file $int_2kref($j) nonewline
puts $file $foo nonewline
puts $file $int_2dref($j) nonewline
}
puts $file $foo
puts $file $int_3aref(1) nonewline
puts $file $foo nonewline
puts $file $int_3kref(1) nonewline
puts $file $foo
puts $file $int_nout nonewline
puts $file $foo nonewline
puts $file $int_dthick nonewline
puts $file $foo nonewline
puts $file $int_nrefine nonewline
puts $file $foo
puts $file $int_cmax nonewline
puts $file $foo nonewline
puts $file $int_cstep nonewline
puts $file $foo
puts $file $int_lensindex nonewline
puts $file $foo
puts $file $int_normtemp nonewline
puts $file $foo nonewline
puts $file $int_dndt nonewline
puts $file $foo
puts $file $int_ray nonewline
puts $file $foo nonewline
puts $file $int_raystep nonewline
close $file
}


Alright, sorry for so many lines guys but i have to be able to output all of the variable input into a *.in file in a nice cozy design pattern.  That's what $foo is for its just a string with a bunch of spaces, because i don't think you can grid during output.  Sorry i'm just a beginner with this.  Anyhow, during output i get an error saying "can't read int_wx(1) : no such variable. " The problem lies in the for loop with the puts commands inside of it i believe.  Anyhow do you know how i can fix this problem?

Posted by admin (Graham Ellis), 16 July 2003
To answer your immediate question ... you could use the info exists command to see if array members exist before you print them out, our you could use the array names command to get the name of all the array elements, and loop through the list that gives you back to print out the elements.

Are you sure that int_nrect contains a count of the number of elements in every array that you're printing out in the for loop that's giving you the problems, and are you sure that the elements are names from 1 up in sequence, with nothing missing?

Aside from your main question, you comment about $foo containing just spaces.   I have a saying "If you find yourself repeating something many times, there must be a better way of doing it", but you've acknowledged that already    ... I would consider writing a proc to output a value and keep calling that, and/or I would consider expanding the strings in double quotes.
Suggestion - fix problems one by one - get the for loop working first, then come back and discuss code tidies  

Posted by Avarice (Avarice), 16 July 2003
Alright i found out the problem.  the array numbers are such as 4 6 2 3 1 5 like that they aren't in order like they should be.  Is there a command that puts them in order that would be convenient or do i have to go through my script and find where they go astray?

Posted by admin (Graham Ellis), 17 July 2003
I expect you have some missing values - otherwise a for loop would pick them out in numerical order... thus:

Code:
set tab(2)  10;
set tab(4)  12;
set tab(6)  14;
set tab(1)  16;
set tab(3)  18;

for {set i 1} {$i <=7} {incr i} {
       puts "Value $i is $tab($i)";
       }


will give an error message because there is no element number 5:

Code:
[graham@dhansak graham]$ tcl tsort1
Value 1 is 16
Value 2 is 10
Value 3 is 18
Value 4 is 12
Error: can't read "tab(5)": no such element in array
   while executing
"puts "Value $i is $tab($i)""
   ("for" body line 2)
   invoked from within
"for {set i 1} {$i <=7} {incr i} {
       puts "Value $i is $tab($i)";
       }
"
   (file "tsort1" line 7)
[graham@dhansak graham]$


If you use array names and a foreach loop, you can get all the elements but in the order they are stored in the array:

Code:
set tab(2)  10;
set tab(4)  12;
set tab(6)  14;
set tab(1)  16;
set tab(3)  18;

foreach i [array names tab] {
       puts "Value $i is $tab($i)";
       }


which runs

Code:
[graham@dhansak graham]$ tcl tsort2
Value 4 is 12
Value 1 is 16
Value 6 is 14
Value 2 is 10
Value 3 is 18
[graham@dhansak graham]$


and finally if you use an lsort, you can sort the list of element names of the array:

Code:
set tab(2)  10;
set tab(4)  12;
set tab(6)  14;
set tab(1)  16;
set tab(3)  18;

foreach i [lsort -integer [array names tab]] {
       puts "Value $i is $tab($i)";
       }


Which gives something like what you want:

Code:
[graham@dhansak graham]$ tcl tsort3
Value 1 is 16
Value 2 is 10
Value 3 is 18
Value 4 is 12
Value 6 is 14
[graham@dhansak graham]$


Note that everything is stored as a string in Tcl and that includes array keys - there is nothing to stop you having a named element in your array, and when sorting the list of names you need to specify the -integer option - otherwise the number 10 would occur between 1 and 2 (for example).

Posted by Avarice (Avarice), 17 July 2003
Thankyou so much.  I have it working now.  However, i did come upon a new problem.  I get an error after 5 or so saves ...  "no space left on device" Is there a way to clear the space after every save?

Posted by Avarice (Avarice), 17 July 2003
Also, is there anyway to align my output any commands so i can strategically place my output.

Posted by admin (Graham Ellis), 18 July 2003
If you're opening a file for write access (with a w), then any file that already exists with the same name should be overwritten and if you keep using the same file name, this shoudn't result in your disc space becoming full.  It could simply be that your web space was nearly full and other activity on your web site filled it - perhaps your site is so popular that your log files are growing huge, for example.

On formatting output, have a look at the format command which lets you format up strings, numbers, etc into fixed length strings which will align nicely when you output them - assuming that you're going to view the output using a fixed width font.





This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho