![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | hinasis |
I’m having a weird result while iterating with dictionaries.
Say I have:
var dict = { d1={v1=0,v2=1}, d2={v1={subv=0} }
So it’s a dictionary of dictionaries, and one of them has another dictionary.
I’m doing so:
for d in dict:
return d.has("v1")
And it throws an error saying it can’t find a “has” method for a String…
String? I say, so I checked it:
print(d)
And it returns "v1"
… the Key, instead of the Value.
It returns the Value if I do:
print(dict[d])
Ok, so I supposed doing for d in dict:
, it returns the keys.
I used to iteract with the keys by doing for key in dict.keys():
…
I thought something was wrong, but I continued, but just after that I do an iteration ,forgetting what I have just done, with the second dictionary in dict
by:
for d in dict:
if dict[d].has("v1"):
if typeof(dict[d].v1) == TYPE_DICTIONARY:
for sub_d in dict[d].v1:
print(sub_d)
And MAGIC!
As you can see, the second for
is iterating in a dictionary but instead of returning the key as the previous example it returned the {subv=0}
as intended.
I don’t know what’s going on…