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

Multidimensionnal Hash table

7 views
Skip to first unread message

RV

unread,
Jan 26, 2006, 7:33:59 AM1/26/06
to
Hello,

I would to use Hash List as we can manipulate easely in Perl :

%Hash{level1}{level2a}
{level2b}...
$Hash{level1}{level2b}=x

and I don't find the syntaxe to use in Tcl to retrieve this,
So, can anybody give me the syntaxe to create and to access variable,
and the way to know if the entry of a hash is an variable or an another
hash entry
, if it exists, in the case of multidimensionnal hash list, of course
???

Thanks

suchenwi

unread,
Jan 26, 2006, 7:41:43 AM1/26/06
to
Tcl arrays (hash tables) map strings to strings. But strings can
express multiple dimensions:
set a(0,0) red
set a(1,1) green
...

Michael Schlenker

unread,
Jan 26, 2006, 7:48:43 AM1/26/06
to
RV schrieb:
Unless your using Tcl 8.5 (alpha) or Tcl 8.4. with the dict package
there is no such datastructure built into Tcl. The 'dict' command
provides nestable hash tables.

In Tcl versions without dict you can fake something like it by using
arrays and lists in a not really straight forward way. (arrays are not
nestable in Tcl because they are collections of variables and not
values). Basically you would use a nested key value list and convert
to/from arrays (hashes) by array get/array set.

But it depends on what you really want to do, for some common problems
datastructures are provided in the struct::* packages in tcllib (tree,
graph, queue, C style records, matrices, ...) for others a decent OO
package like snit or XOTcl may be the right choice.

Michael

RV

unread,
Jan 26, 2006, 7:57:31 AM1/26/06
to
I'm using Tcl/TK 8.4, may be in using 'dict', i'll find what I want ??

sleb...@gmail.com

unread,
Jan 26, 2006, 8:03:57 AM1/26/06
to
suchenwi wrote:
> RV wrote:
> > I would [like] to use Hash List as we can manipulate easely in Perl :
> > %Hash{level1}{level2a}
> > $Hash{level1}{level2b}=x

Or, as pointed out in another therad not too long ago, we can also
directly emulate the Perl 'syntax' that RV is used to. Tcl happily
accepts the following:

set a(level1)(level2a) x
set a(level1)(level2b) y

just try it. For those who may be wondering how the above code can work
here's a hint: Suchenwi used the comma (,) as a pseudo delimiter for
each dimension, what delimiter did I use (further hint: my delimiter is
not a single character).

Bryan Oakley

unread,
Jan 26, 2006, 8:44:32 AM1/26/06
to
RV wrote:
> I'm using Tcl/TK 8.4, may be in using 'dict', i'll find what I want ??
>

Why do arrays not work for you? Not that I have anything against dict --
it's great, but 8.5 is still in alpha if that matters to you.

Based on your original question it looks like arrays would work just
fine. It's a very common idiom to use pseduo-multidimensional arrays
using a separator character of your choice (foo(a,b) foo(a:b), foo(a'b),
etc.

You can't store an array in an array, but rarely, if ever, should you
need to do that.

Michael Schlenker

unread,
Jan 26, 2006, 8:24:14 AM1/26/06
to
RV schrieb:

> I'm using Tcl/TK 8.4, may be in using 'dict', i'll find what I want ??
>
Try it, the dict for Tcl 8.4 package is linked here:
http://wiki.tcl.tk/dict

(near the top of the page)

Michael

RV

unread,
Jan 26, 2006, 8:31:39 AM1/26/06
to

> Tcl happily
> accepts the following:
>
> set a(level1)(level2a) x
> set a(level1)(level2b) y
>
> just try it.

I tried but I didn't manged to access the value.

puts [$a(level1)(level2a)]

->can't read "a(level1)" : no such element in array

RV

unread,
Jan 26, 2006, 8:39:43 AM1/26/06
to
In fact, I'm trying to create an easy access database like I made in
Perl for collecting data on a networks, sorted by switches, machines,
Vlans ... It's not what I have to do now, but for this application, I
founnd that was very usefull and I try to retrieve this TCL .
The advantage is that we can stock anything with beautiful
understandable string path.

Bryan Oakley

unread,
Jan 26, 2006, 9:06:10 AM1/26/06
to

I think that was bad advice. It is possible to use anything for an index
(including words with spaces) but it requires extra quoting which isn't
usually worth the effort. Don't use the double-parenthesis version.
Using commas and other non-special characters work fine.

BTW: the square brackets almost certainly don't do what you expect in
the above snippet.

sleb...@gmail.com

unread,
Jan 26, 2006, 9:29:41 PM1/26/06
to
RV wrote:
> > Tcl happily
> > accepts the following:
> >
> > set a(level1)(level2a) x
> > set a(level1)(level2b) y
> >
> > just try it.
>
> I tried but I didn't manged to access the value.
>
> puts [$a(level1)(level2a)]

you mean:

puts $a(level1)(level2a)

Sorry, I didn't test variable substitution. It seems that the $
substitution doesn't parse this the same way the set command does. It
works with:

puts [set a(level1)(level2a)]

but I guess you'd be better off with the comma delimited version:

set a(level1,level2a) x
puts $a(level1,level2a)

The following versions also work well:

set a(level1.level2a) x
set a(level1:level2a) x

basically, choose a character that the $ substitution doesn't recognise
as your delimiter.

suchenwi

unread,
Jan 27, 2006, 2:23:21 AM1/27/06
to
To be more precise, the $ parser takes anything between ( and ) for an
array key:
25 % set a(%&/?) 42
42
21 % array names a
%&/?
while with variable names themselves, [A-Za-z0-9_]+ is your best bet.
But for variables in the key itself, which are substituted before the
array reference, the suggestion is good:
set a($x,$y) 1
puts $a($x,$y)

Had you taken "_" as delimiter, you'd need
set a(${x}_$y) 1
because otherwise parsing the "x" runs on into the "_", and possibly
"unknown variable x" may be reported.

Michael Schlenker

unread,
Feb 1, 2006, 2:07:46 AM2/1/06
to
RV schrieb:
I hope you already took a look at 'moodss', sounds like something you
could find useful.

Michael

0 new messages