Map

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Map

Post 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()
Last edited by mestnyi on Wed Dec 18, 2024 9:18 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Map

Post 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.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Map

Post 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()
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Map

Post 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.
Last edited by infratec on Wed Dec 18, 2024 9:37 am, edited 2 times in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Map

Post by mk-soft »

The behavior is correct with ForEach, because the loop ends when there is no more current element
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Map

Post 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
 
Post Reply