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
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
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).
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.
(near the top of the page)
Michael
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
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.
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.
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