Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/how-to-store-data-in-array.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
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))
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.

© 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