From: Donal K. Fellows on 27 Apr 2010 06:50 On 26 Apr, 23:31, cattaghia <cattag...(a)ig.com.br> wrote: > Hmm. So I presume that "dict exists" checks this pairs "strict > condition" on its argument before actually looking for a certain key. > If the list does not have an even number of items, boOoOom. > > Not bad, really. But should not the documentation tell about the > possibility of an error if a bad-formatted dict is thrown on it? [dict exists $foo a b] is the same as [dict exists [dict get $foo a] b] Donal.
From: Bruce on 27 Apr 2010 15:36 tom.rmadilo wrote: > On Apr 26, 3:31 pm, cattaghia <cattag...(a)ig.com.br> wrote: >> Hmm. So I presume that "dict exists" checks this pairs "strict >> condition" on its argument before actually looking for a certain key. >> If the list does not have an even number of items, boOoOom. >> >> Not bad, really. But should not the documentation tell about the >> possibility of an error if a bad-formatted dict is thrown on it? > > I'm not a real user of dict, but I have run a number of experiments. > Basically it is a portable, hierarchical Tcl array. But only the top > level can be tested for an odd number of values !(len & 1). For yep that test the main list is a "valid" dict if you want to test if any values in the dict are (or can be) dicts as well, then you need to check them (or handle errors) > instance dict x: {a {b c} d {e f g} h {i j k l}} passes the top level > test. Path {a b} exists, path {d e} is invalid, path {h k} is valid. > If you [dict lappend x d m], then path {d e} is now valid and returns "f". > Not clear to me is why the [dict lappend x d m] doesn't create a > dict {a {b c} d {{e f g} m} h {i j k l}}, but that is the way it > works. for the same reason set y [list a b c] lappend y d gives {a b c d} and not {{a b c} d} the values in a dict *can* be another dict but it doesn't have to the subcommands tell you (in the docs and generally by the names) what it expects the value to be (or rather how it will be treated) e.g. quoting <http://www.tcl.tk/man/tcl8.5/TclCmd/dict.htm#M17> dict lappend dictionaryVariable key ?value ...? This appends the given items to the list value that the given key maps to in the dictionary value contained in the given variable, writing the resulting dictionary value back to that variable. Non-existent keys are treated as if they map to an empty list, and it is legal for there to be no items to append to the list. It is an error for the value that the key maps to to not be representable as a list. bruce
From: Arnold Snarb on 28 Apr 2010 18:12
Cattaghia asked: > > Hmm. So I presume that "dict exists" checks this pairs "strict > condition" on its argument before actually looking for a certain key. > If the list does not have an even number of items, boOoOom. > > Not bad, really. But should not the documentation tell about the > possibility of an error if a bad-formatted dict is thrown on it? The documentation already says that. You just aren't reading the documentation correctly. TFM sez: | dict exists dictionaryValue key ?key ...? ...............^^^^^^^^^^^^^^^ <=== CLUE HERE!!! | This returns a boolean value indicating whether the given key | (or path of keys through a set of nested dictionaries) exists | in the given dictionary value. This returns a true value exactly .........^^^^^^^^^^^^^^^^^^^^^^^^^^ <=== CLUE HERE!!!! | when dict get on that path will succeed. When the manpage says things like "blah blah blah dict exists *dictionaryValue* blah blah blah exists in the given *dictionary value* blah blah blah", it means that you are expected to provide a *dictionary value* in those places. Now if you don't provide a dictionary value in the places where TFM says that it's expecting to see one -- f'rinstance if you ask [dict exists "I am Not A Dictionary" $key] -- you'll get an error. You should expect this. You should *not* expect TFM to explicitly and redundantly and verbosely and redundantly explain to you all of the things that might possibly go wrong if you do something that isn't prescribed or described therein. --Arnold (once mispelt [dict exists $dv $k] as [dcit exits $vd $K], and filed a documentation bug.) |