Tcl8.5: [string map $dict ...] and ordering inside $dict 
Author Message
 Tcl8.5: [string map $dict ...] and ordering inside $dict

With the new "dict", I was expecting [string map ...] to also
use dicts, but with the latest change (in CVS) of string's
manpage, it is clarified, that order of "key value"-pairs *does*
matter, which would make the use of dicts quite undetermined,
at least if there exist two keys in a dict one of which is a
prefix of the other one.

Even if I can be sure that no such substring-of-other key
exists in a certain dict, it'll be quite likely that any
dict will be added (or replaced by) a list-rep during such
an operation.

Is this an issue, or are dicts a bad choice anyway to pass to
[string map ...] for other reasons I'm not currently aware of?

Wouldn't it be better to document "substring-of-other" keys as
"bad thing" that might cause undetermined behaviour?



Sun, 11 Feb 2007 20:03:50 GMT  
 Tcl8.5: [string map $dict ...] and ordering inside $dict
Quote:

> With the new "dict", I was expecting [string map ...] to also
> use dicts, but with the latest change (in CVS) of string's
> manpage, it is clarified, that order of "key value"-pairs *does*
> matter, which would make the use of dicts quite undetermined,
> at least if there exist two keys in a dict one of which is a
> prefix of the other one.

> Even if I can be sure that no such substring-of-other key
> exists in a certain dict, it'll be quite likely that any
> dict will be added (or replaced by) a list-rep during such
> an operation.

> Is this an issue, or are dicts a bad choice anyway to pass to
> [string map ...] for other reasons I'm not currently aware of?

> Wouldn't it be better to document "substring-of-other" keys as
> "bad thing" that might cause undetermined behaviour?

add on:
docu clearly states, that the "charMap is a list...", but it
further says: "as in the form returned by array get", which
happens to be a dict now ;-)

Would it be sensible to autosort it such that substrings are
re-ordered to always appear after the key that they are substring
of?  Or would this make the whole thing too inefficient?

Among keys not substring of any others,  the order does not matter
at all, so it may be possible to "pre-sort" them by descending
string-length.



Sun, 11 Feb 2007 20:14:17 GMT  
 Tcl8.5: [string map $dict ...] and ordering inside $dict

Quote:

> Is this an issue, or are dicts a bad choice anyway to pass to
> [string map ...] for other reasons I'm not currently aware of?

Not a (major) issue.  Shimmering between lists and dicts is handled
specially, and won't have any affect if you satify the
"substring-of-other" condition you noticed.  Of course, if the
condition isn't satisfied you'll have a problem, so don't do that. :^)

BTW, it was annoying to me that accepting a dict as direct input
couldn't be done, but it can't really be helped without a significant
change to [string map].  I really can't justify doing that to myself,
let alone anyone else.  Sorry that my initial plans didn't work, but
that sometimes happens and I believe the current solution is actually
better in practice.

Quote:
> Wouldn't it be better to document "substring-of-other" keys as
> "bad thing" that might cause undetermined behaviour?

The order of keys in a dictionary is documented as being undetermined,
and the order of source strings to [string map] is documented as being
significant when some source keys are prefixes of others, so it is
trivial to go from that to seeing that feeding a dict into [string
map] can cause problems in the general case.  Whether this consequence
needs specific documentation remains to be seen.

Donal.



Mon, 12 Feb 2007 16:44:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dict to String and String to Dict with Visual Works

2. expanding a list and dict to a *args and **dict

3. Dict can't update with dict-like objects

4. NewBuiltins: added dict() -- inverse of dict.items()

5. Builtin dict should be callable, since a dict defines a funct ion

6. Builtin dict should be callable, since a dict defines a function

7. [dict] command backport for tcl8.4

8. Failure on dict test - looking at Tcl8.5a0 thru Borland's eyes

9. map-like function on dict values?

10. need dict to maintain order

11. Iterating order of dict.items()

12. Dict. to String and String to Dict. with Visual Works

 

 
Powered by phpBB® Forum Software