Strange dict behavior part 2
- From: Bezoar <cwjolly@xxxxxxxxx>
- Date: Sun, 3 Jun 2012 12:59:25 -0700 (PDT)
Disregard prior post... browser was twitchy.
I was trying a recursive routine to pull out all the keys of a dict and the results differed depending on how I initialized the data in the dict. My results were the same if I accessed the value of a key first then got all the keys vs getting all the keys. A snippet below shows the situation on tcl8..6b2.
==== code ==
proc getType { typestring} {
string range [string range $typestring [string first " a " $typestring ] [string first " with " $typestring ] ] 2 end-1
}
set input [dict create home {} ]
dict set input1 home address { street1 "23 Ave" street2 "Suite 200" }
dict set input home address street1 "6543 Waldorf Plaza"
dict set input home address street2 "Suite 200"
foreach key { home { home address } { home address street1 } { home address street2 } } {
set rep1 [ ::tcl::unsupported::representation [dict get $input1 {*}$key ] ]
set rep [ ::tcl::unsupported::representation [dict get $input {*}$key ] ]
if { [ getType $rep1 ] ne [ getType $rep ] } {
puts "representation for key '$key' in input1 is [ getType $rep1 ] but same key in input is [getType $rep ]"
}
# puts "[format "%50s | %50s" [dict get $input1 {*}$key ] [ dict get $input {*}$key ] ]"
}
If I run the code above I get the following results :
---- output ---
representation for key ' home address ' in input1 is pure string but same key in input is dict
---- end output ----
if I uncoment the last puts statement, I get the following
---- output ---
address { street1 "23 Ave" street2 "Suite 200" } | address {street1 {6543 Waldorf Plaza} street2 {Suite 200}}
street1 "23 Ave" street2 "Suite 200" | street1 {6543 Waldorf Plaza} street2 {Suite 200}
23 Ave | 6543 Waldorf Plaza
Suite 200 | Suite 200
---- end output ----
Why is this? and is it a bug. I would like to get the keys for a dict without knowning the keys in the first place. This seems not to be possible unless you force the user to not use a list to initialize a dict.
Thanks
Carl
.
- Follow-Ups:
- Re: Strange dict behavior part 2
- From: mango
- Re: Strange dict behavior part 2
- From: Gerald W. Lester
- Re: Strange dict behavior part 2
- Prev by Date: strange dict behavior
- Next by Date: Re: strange dict behavior
- Previous by thread: strange dict behavior
- Next by thread: Re: Strange dict behavior part 2
- Index(es):
Relevant Pages
|