Page 1 of 1

Map

Posted: Wed Dec 18, 2024 8:54 am
by mestnyi
Is this the expected behavior? shouldn't the last element have remained unchanged when exiting the loop?

Code: Select all

NewMap Country.s()

Country("GE") = "Germany"
Country("FR") = "France"
Country("UK") = "United Kingdom"

Debug "good "+Country()

ForEach Country()
   Debug Country()
Next

; ResetMap(Country())
; NextMapElement(Country())

Debug " badly "+Country()

Re: Map

Posted: Wed Dec 18, 2024 8:59 am
by infratec
No.

After a ForEach Map or List the pointer is always behind, else the loop is not finished.
Never is the last element selected.

Re: Map

Posted: Wed Dec 18, 2024 9:15 am
by mestnyi
infratec wrote: Wed Dec 18, 2024 8:59 am No.

After a ForEach Map or List the pointer is always behind, else the loop is not finished.
Never is the last element selected.
I don't agree with you. This is the expected behavior for me. :)

Code: Select all

NewList Country()

AddElement(Country())
Country() = 1
AddElement(Country())
Country() = 2
AddElement(Country())
Country() = 3

Debug "good "+Country()

ForEach Country()
   Debug Country()
Next

Debug " good "+Country()

Re: Map

Posted: Wed Dec 18, 2024 9:33 am
by infratec
If I follow your opinion, the following code should show 9

Code: Select all

For i = 0 To 9
Next i

Debug i
But it shows 10, which is behind the loop counter.

You can try to place a bug report about this :wink:

I often use if and break inside loops and detect behind the loop
if the loop was 'normally' finished or 'breaked'
With a normal For you can check if the vaiable is higher then the last wanted value.
With a ForEach you can check this if an element is selected.

Re: Map

Posted: Wed Dec 18, 2024 9:35 am
by mk-soft
The behavior is correct with ForEach, because the loop ends when there is no more current element

Re: Map

Posted: Wed Dec 18, 2024 9:40 am
by mestnyi
mk-soft wrote: Wed Dec 18, 2024 9:35 am The behavior is correct with ForEach, because the loop ends when there is no more current element
Why does this behavior not belong to the "list"?
in this regard, I had to add this.

Code: Select all

; bug PB
            If MapKey(roots( )) = ""
               ResetMap(roots( ))
               NextMapElement(roots( ))
            EndIf