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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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:

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
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 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., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho