Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[newbie] array of array ?

0 views
Skip to first unread message

barthelemy...@gmail.com

unread,
Jan 8, 2009, 6:05:40 AM1/8/09
to
Hello,

I tried to have an array containing an array as value :
<module_name <file_name result> >

This really makes sense for me to have such a structure (I am testing
several times a module with different files and I want to store the
result for each file of each module).

I do :
# set elements in files_results array
tcl>set files_results("file1") "OK"
# then put this array in another array
tcl>set modules_results("module1") $files_results
Error: can't read "files_results": variable is array

First question : is it allowed to do that ?
Second question : if not, how should I do ?
Optional question : why is it forbidden ? ? ? ?

I use tcl 8.4.

Thanks in advance

Barth

Andreas Leitgeb

unread,
Jan 8, 2009, 6:44:17 AM1/8/09
to
barthelemy...@gmail.com <barthelemy...@gmail.com> wrote:
> # set elements in files_results array
> tcl>set files_results("file1") "OK"
> # then put this array in another array
> tcl>set modules_results("module1") $files_results
> Error: can't read "files_results": variable is array

set modules_results("module1") [array get files_results]

This will obtain the contents of the array as a value, which
then can be stored into another array. While it is thusly
stored away, it is not an array itself, so if you have traces
on files_results (or it's elements), that you need to keep up,
then you need a different alternative.

Later, you can do:
array unset tmp; # make sure it's empty
array set tmp $modules_results("module1")
puts $tmp("file1");# prints OK

> Optional question : why is it forbidden ? ? ? ?

arrays (and also variables) can *store* values, but *aren't* values,
themselves.

Btw., as you wrote it, the double-quote chars inside the parentheses
become literally part of the keys. Not sure if this is intended.
Probably better:

set files_results(file1) "OK"
set modules_results(module1) [array get files_results]
#later:
array unset curfile;# if the array may pre-exist
array set curfile $modules_results(module1)
puts $curfile(file1);# --> OK

> I use tcl 8.4.

If you upgraded to 8.5, then the "#later:"-part above can
be also written such:

set curmod $modules_results(module1); # normal variable
puts [dict get $curmod file1] ;# no ()-syntax here

Donal K. Fellows

unread,
Jan 8, 2009, 6:50:17 AM1/8/09
to
On 8 Jan, 11:05, barthelemy.von.hal...@gmail.com wrote:
> First question : is it allowed to do that ?
> Second question : if not, how should I do ?
> Optional question : why is it forbidden ? ? ? ?
>
> I use tcl 8.4.

Were you using a later version, I'd advise dicts. But for 8.4 (and
before) you have to use a trick. Array element names can be as complex
a string as you like, including having a comma between sub-parts:

set modules_results(module1,file1) OK

If that doesn't do for you, then you have the other alternative. The
commands [array get] and [array set] are used to convert between
arrays and a serialized string form (that happens to be a dictionary
in 8.5).

set files_results(file1) OK
set modules_results(module1) [array get files_results]

(The thing you were trying is illegal because arrays aren't values,
but rather collections of variables. In Tcl, those are fundamentally
different. Variables, including array elements, can only hold values.)

Donal.

barthelemy...@gmail.com

unread,
Jan 8, 2009, 6:59:35 AM1/8/09
to
Thank you for your help and explanations !

I will go for the second solution (complex string)
as it is enough for what I want to do.

Just another question, in the tcl interpreter I can't use the arrows,
delete or backspace keys, they just print stuff like ^[[B. How should
I do to be able to use those keys ? I am working on Red Hat
Enterprise.

Barth

Bryan Oakley

unread,
Jan 8, 2009, 7:05:19 AM1/8/09
to
On Jan 8, 5:05 am, barthelemy.von.hal...@gmail.com wrote:
> Hello,
>
> I tried to have an array containing an array as value :
> <module_name <file_name result> >
>
> This really makes sense for me to have such a structure (I am testing
> several times a module with different files and I want to store the
> result for each file of each module).
>
> I do :
> # set elements in files_results array
> tcl>set files_results("file1") "OK"
> # then put this array in another array
> tcl>set modules_results("module1") $files_results
> Error: can't read "files_results": variable is array
>

Others have suggested how to use array get / array set to store one
array in another. Might I offer another solution? Use a single array
with a complex key:

set results(module1,file1) "OK"
set results(module1,file2) "OK"
set results(module2,file1) "OK"
...

This is much easier than to juggle arrays with array get / array set.

Donal K. Fellows

unread,
Jan 8, 2009, 9:04:06 AM1/8/09
to
On 8 Jan, 11:59, barthelemy.von.hal...@gmail.com wrote:
> Just another question, in the tcl interpreter I can't use the arrows,
> delete or backspace keys, they just print stuff like ^[[B. How should
> I do to be able to use those keys ? I am working on Red Hat
> Enterprise.

That's a simple one. Tclsh isn't built with readline support, so
you've got a basic terminal (which doesn't understand arrow keys)
rather than the highly enhanced one. This is for (nasty ugly) license
reasons.

You can work around this by getting tclreadline. Or you can use the
tkcon application (which is what I do for most of my interactive
stuff) which is even better as long as you have X11 running.

Donal.

google...@rocketship1.biz

unread,
Jan 9, 2009, 7:23:39 PM1/9/09
to
On Jan 8, 6:04 am, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
wrote:

While tkcon is very powerful, it's not as simple as the console on
windows. Tkcon has always been a bit of a puzzle for me, given the
menu commands for selecting the right interpreter etc.

This link is about enabling the windows console on linux:

http://wiki.tcl.tk/786

0 new messages