Can anyone explain the difference between array and keyed list? In my perspective both are doing same.
can anyone differentiate these? Which is best to use?
Both are conceptually associative maps.
A Tcl array is a compound variable that is a collection of simple variables. Each variable may hold any value (no, variables aren't values) and the array is indexed by arbitrary values. There is no defined order of keys. As with any variable, you can set a trace on an array or on any element of an array. You can also vwait on them.
A TclX keyed list is a value that holds other values, and it is indexed by values (with some restrictions because . is used as a structured index separator). I think the key order is “insertion order” but I'm not 100% on that. Keyed list elements have no particular identity.
You can put a keyed list in an array element, but you can't put an array in a keyed list at all.
Which is best to use? It really depends on what you're doing. Arrays are built into Tcl itself; you've always got them. Keyed lists depend on the TclX package being present.
There's also dictionary values built into Tcl from 8.5 onwards; they've got a different syntax to keyed lists for reasons that are a little unfortunate. However, they also have a lot more support commands and they support bytecode compilation, making dictionaries rather faster than keyed lists. You've still got arrays though, as arrays aren't values and dictionaries aren't variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With