DeleteMapElement while iterating with ForEach

Just starting out? Need help? Post your questions and find answers here.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

DeleteMapElement while iterating with ForEach

Post by Xanos »

What is the best method to delete map elements while being in a ForEach map() loop?
Example code, that is not working:

Code: Select all

Global NewMap test()

For i = 0 To 9
  r = Random(100)
  test(Str(r)) = r
Next


ForEach test()
  Debug "check element "+MapKey(test())
  If test() > 50
    DeleteMapElement(test(), MapKey(test()))
  EndIf
Next

Debug "---"

ForEach test()
  Debug MapKey(test())
Next
As soon as the first element is deleted, the ForEach loop just exists.
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: DeleteMapElement while iterating with ForEach

Post by Josh »

Code: Select all

Global NewMap test()

For i = 0 To 9
  r = Random(100)
  test(Str(r)) = r
Next


ForEach test()
  Debug "check element "+MapKey(test())
  If test() > 50
    DeleteMapElement(test())
  EndIf
Next

Debug "---"

ForEach test()
  Debug MapKey(test())
Next
sorry for my bad english
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: DeleteMapElement while iterating with ForEach

Post by Xanos »

So in short, it is not possible to delete MapElements by stating their "Key$" in a ForEach?
Unfortunately, I have to rewrite a bunch of code to get around this in the real project (obviously not the short example code).
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: DeleteMapElement while iterating with ForEach

Post by Josh »

Xanos wrote:So in short, it is not possible to delete MapElements by stating their "Key$" in a ForEach?
Help readers know more :mrgreen:
Help wrote: DeleteMapElement()
If the optional 'Key$' parameter is specified then there will be no more current element after this call.
sorry for my bad english
normeus
Enthusiast
Enthusiast
Posts: 472
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: DeleteMapElement while iterating with ForEach

Post by normeus »

use push and pop: *EDIT working code with push and pop

Code: Select all

Global NewMap test()

For i = 0 To 9
  r = Random(100)
  test(Str(r)) = r
Next

ForEach test()
  Debug MapKey(test())
Next

ForEach test()

  PushMapPosition(test())
  While NextMapElement(test())
    If test() > 50
      DeleteMapElement(test())
    EndIf
  Wend
  
  PopMapPosition(test())
Next
; "--- NECESSARY EVIL because we skipped first element do test on first again"
ResetMap(test())
NextMapElement(test())
If test() > 50
  DeleteMapElement(test())
EndIf

Debug "---"

ForEach test()
  Debug MapKey(test())
Next
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: DeleteMapElement while iterating with ForEach

Post by Josh »

normeus wrote:use push and pop
No need to use push and pop. Like I wrote in my example, only delete the second parameter in 'DeleteMapElement'
sorry for my bad english
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: DeleteMapElement while iterating with ForEach

Post by Xanos »

Not using the second argument was not an option (at least not easily done), so pop & push are valid solutions for this problem :)
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
Post Reply