[Gambas-user] Collections
Werner
wdahn at ...1000...
Sat Jan 16 12:40:41 CET 2010
On 16/01/10 18:23, Pino Zollo wrote:
> Il venerdì 15 gennaio 2010 17:39:14 hai scritto:
>
>> Am Freitag, den 15.01.2010, 16:04 -0300 schrieb Pino Zollo:
>>
>>> DIM Dict AS NEW Collection
>>> DIM Element AS String
>>>
>>> Dict["Blue"] = 3
>>> Dict["Red"] = 1
>>> Dict["Green"] = 2
>>>
>>> FOR EACH Element IN Dict
>>> PRINT Element;
>>> NEXT
>>>
>> Salut,
>>
>> did you ever more then run that code ? debug ? or looked whats a
>> Collection ?
>>
>> OK, "Blue","Red" and "Green" are the KEY for the Collection Element,
>> and so they declared and asigned.
>>
>> try that!
>>
>> PUBLIC SUB Main()
>> DIM Dict AS NEW Collection
>> DIM Element AS String
>>
>> Dict["Blue"] = 3
>> Dict["Red"] = 1
>> Dict["Green"] = 2
>>
>> FOR EACH Element IN Dict
>> PRINT Element
>> PRINT Dict.Key
>> NEXT
>> END
>>
> Thanks Charlie,
> I was trying to understand Collections better as I have a wrong behaviour in the following piece of code
> when I use an array of two strings as element; the key is an unique string.
>
> The two print statements print exactly the same thing...the Lista.key is correct and all different, but the
> other ( ; Element[1];; " ";; Element[0] ) print always the last element added to the structure.
>
> If instead of using an Array[2] I use a single String joining the two strings into a single one, then all works
> perfectly....
>
> ...so I guess that there is a bug in how Structure handles Variant elements.
>
> Just another example of my bad luck !
> ....or maybe I do not understand something.
>
> Best regards
>
> Pino
> ---------------------------------------------
> DO WHILE i < MyArray.Count
> myStruc = MyArray.Pop()
> a = 0
> DO WHILE a < myStruc.Count
> ' DEBUG myStruc.Key(a);; myStruc.Value(a) ' myStruc.dataType(a)
> Disp.DisRes.Text = Disp.DisRes.Text & myStruc.Key(a) & ": " & myStruc.Value(a)
> WAIT
> Disp.DisRes.Pos = Len(Disp.DisRes.Text)
> Disp.DisRes.EnsureVisible()
> INC a
> LOOP
> Parti[0] = myStruc.Value(0)
> Parti[1] = myStruc.Value(2)
> ' DEBUG Parti[0];; Parti[1];; myStruc.Value(1)
> ' Lista.Add(Parti, myStruc.Value(1)) ' acts same as the following
> Lista[myStruc.Value(1)] = Parti
> DEBUG Lista.Count;; Lista[myStruc.Value(1)][1];; " ";; Lista[myStruc.Value(1)][0]
> Disp.DisRes.Text = Disp.DisRes.Text & "~~~~~~~~~"
> INC i
> LOOP
> ' CBCommand.Clear
> ' CBComm2.Clear
> FOR EACH Element IN Lista
>
> PRINT Lista.Key;; " ";; Lista[Lista.key][1];; " ";; Lista[Lista.key][0]
> PRINT Lista.Key;; " ";; Element[1];; " ";; Element[0]
>
> ' SELECT CASE Element[1]
> ' CASE "n:s"
> ' CBCommand.Add(Lista.Key)
> ' CASE "s:n"
> ' CBComm2.Add(Lista.Key)
> ' END SELECT
> NEXT
> -----------------------------------------------------
>
>
I look at the code above and wonder abount the first 2 lines.
Say i is 3 and count is 6. When it loops you get the following:
i count
3 6
4 5
end of loop
because the Pop in the second line reduces count. Is that what you wanted?
Regards
Werner
More information about the User
mailing list