| |||||||||||
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:
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:
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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |