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

Multidimensional arrays in TCL

1,319 views
Skip to first unread message

Cassy

unread,
Aug 3, 2005, 9:29:04 AM8/3/05
to
Please can anybody tell me how to implement a 4-dimensional array in
TCL and I should be able to access any element of that array at any
point of time, by simple indexing like in C but here it should be able
to dynamically support any-sized strings without memory allocation.

Please let me know fast if possible.

Thanks.

Arjen Markus

unread,
Aug 3, 2005, 9:38:55 AM8/3/05
to
You can use a composite array index, like:

set array(1,2,3,4) "My precious data"
set array(2,1,3,10) "Oh dear"

You can also use a list of lists of lists of lists:

set data {
{
{
{1 2 3}
{3 4 5}
}
{
{A V C}
{a b c d}
(a b c d e f g}
}
}
{
{
{A}
}
}
{
...
}
}

puts [lindex $data 1 2 3 4]

If you prefer random access, then an (associative) array like the first
will do nicely, I think (or a dict if you use Tcl 8.5).

If you need a more structured access, like looping over the various
dimensions, then I suggest you use nested lists. For an example of
those: see the Wiki (http://wiki.tcl.tk) and look for the page on
matrix operations ..

Cassy

unread,
Aug 3, 2005, 10:18:03 AM8/3/05
to
Thanks for your reply, but I found a command called "narray", can that
be used ?

I guess this has the C-kind of loop handling with indices for
manipulation.

I shall check, please let me know if you have seen this before ...

Cassy

unread,
Aug 3, 2005, 3:25:26 PM8/3/05
to
Can somebody give me the link to "narray" implementation (software), it
was supposed to be located at ftp://overload.lbl.gov/pub/narray/ as
mentioned by
http://www.csua.berkeley.edu/~sls/narray/ .

But is not accesible, please do let me know, if somebody knows its
location.

Thanks, I would be really grateful.

Aric Bills

unread,
Aug 3, 2005, 4:20:53 PM8/3/05
to

Judging from the web page I looked at, this extension is pretty dead.
As far as I can tell, it hasn't been updated in at least 11 years and
never made it past alpha stage. Those factors pretty much guarantee
that it won't work for you.

Have you looked at NAP? I can't guarantee that it will do the job
either, but at least it's actively maintained :)

http://mini.net/tcl/nap

David N. Welton

unread,
Aug 4, 2005, 3:36:14 AM8/4/05
to
For multi-dimensional arrays, you either want to use lists of lists, as
has been mentioned. Those are like C-style arrays that you access like
foo[1][10]. If you want to access via keys (i.e. a hash table), you can
use the 'dict' system that will be part of 8.5 - there is a backport to
8.4 if you search around for it.

--
David N. Welton
- http://www.dedasys.com/davidw/

Apache, Linux, Tcl Consulting
- http://www.dedasys.com/

Cassy

unread,
Aug 4, 2005, 3:58:12 AM8/4/05
to
Hi David and everybody else,

Thanks for your messages ...

I am still not able to see the beauty (correlation between) of lists of
lists and the C-style arrays. Maybe an example in TCL for 4-dimensions
and the C-style equivalent can be shown.

Also this kind of lists should be expansible dynamically as I will be
filling these lists dynamically when I am parsing a result file that
contains a lot of strings. These strings are to be arranged into the
4-D array in a specific way.

Thanks, please give me an illustration.

Aric Bills

unread,
Aug 4, 2005, 4:50:49 AM8/4/05
to

Here you go.

C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {

char mylist[2][2][2][2][5];

strcpy(mylist[0][0][0][0], "aaaa");
strcpy(mylist[0][0][0][1], "bbbb");
strcpy(mylist[0][0][1][0], "cccc");
strcpy(mylist[0][0][1][1], "dddd");
strcpy(mylist[0][1][0][0], "eeee");
strcpy(mylist[0][1][0][1], "ffff");
strcpy(mylist[0][1][1][0], "gggg");
strcpy(mylist[0][1][1][1], "hhhh");
strcpy(mylist[1][0][0][0], "iiii");
strcpy(mylist[1][0][0][1], "jjjj");
strcpy(mylist[1][0][1][0], "kkkk");
strcpy(mylist[1][0][1][1], "llll");
strcpy(mylist[1][1][0][0], "mmmm");
strcpy(mylist[1][1][0][1], "nnnn");
strcpy(mylist[1][1][1][0], "oooo");
strcpy(mylist[1][1][1][1], "pppp");

printf("%s\n", mylist[0][1][0][1]);

exit(EXIT_SUCCESS);
}


Tcl:

set mylist {{{{aaaa bbbb} {cccc dddd}} {{eeee ffff} {gggg hhhh}}}
{{{iiii jjjj} {kkkk llll}} {{mmmm nnnn} {oooo pppp}}}}

puts [lindex $mylist 0 1 0 1]

I'll leave dynamic list expansion as an exercise for the reader. If
you're stumped, try reading the man pages for the commands that start
with the letter L.

Arjen Markus

unread,
Aug 4, 2005, 6:59:53 AM8/4/05
to
NAP is an excellent alternative - especially if you have loads of
homogeneous data (either strings or numbers I mean). At least have a
look at it.

Regards,

Arjen

0 new messages