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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
how to store data in array

Posted by dee00zee (dee00zee), 30 March 2005
hi im new here and new to tcl too..
i would like to know how to store data in an array
let say I read a line from a file, and I want to store that line in an array so that I could access the individual element in the array an print them, how to do that in tcl?

Posted by admin (Graham Ellis), 30 March 2005
Tcl includes both lists and arrays, and for what you describe a list is probably more appropriate.   Arrays in Tcl are tylically keyed by name not by number.

When you read a line into a Tcl variable you can treat it as a list straight away using commands like llength and lindex. The list command itself can help with the reformatting if the incoming lines contain special characters, and split might be useful to.

Simple example:

Code:
set fhandle [open dnsdata r]

while {[gets $fhandle inline] >= 0} {
       switch -glob -- $inline {
       Name:*  {
               set name [lindex $inline 1]
               }
       Address:* {
               set address($name) [lindex $inline 1]
               }
       }
}
close $fhandle


Opens a file called dnsdata, reads it line by line, and saves the second field on each line as a name or as an address depending on whether the first field was "Name:" or "Addresss:"

inline is a list, even though you don't declare it as such. And a Tcl list is rather like an array in other languages!

Posted by neo (The Architect), 1 May 2005
How do you print the individual elements in an Array?
I tried
Code:
foreach arg $arr {
         puts stdout $arg
}

but got an error with it.

Just used this thread because it seemed related.


Posted by admin (Graham Ellis), 1 May 2005
That's the syntax for a list  

Use the array names command to get the names of all the elements in an array if you want to iterate over it

Posted by neo (The Architect), 1 May 2005
Ok thanks i'll try that.
BTW anyway we can convert a list to an Array?



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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho