Page 1 of 1

More commands for Maps

Posted: Mon May 16, 2011 10:52 pm
by Seymour Clufley
ChangeCurrentMapElement()
- set current map element by pointer

PreviousMapElement()
- opposite of NextMapElement()

Specify a key with DeleteMapElement() and ensure the next element becomes current afterwards. For example, inside a ForEach loop, you could delete a specified element without breaking the loop.

Re: More commands for Maps

Posted: Mon May 16, 2011 11:46 pm
by Demivec
Seymour Clufley wrote:Specify a key with DeleteMapElement() and ensure the next element becomes current afterwards. For example, inside a ForEach loop, you could delete a specified element without breaking the loop.
Why would you want a new element to become current if you are deleting them using a key anyway? For the loop sake it can be done with existing commands (for reference):

Code: Select all

ForEach test()
  If test() = x Or Left(MapKey(test()), 1) = Left(Str(x), 1) ;use either the MapKey() or map value to delete element
    DeleteMapElement(test())
  EndIf
Next
Regarding the request for ChangeCurrentMapElement(), is the need for that met by the newest additions of PushMapPosition() and PopMapPosition()?

Re: More commands for Maps

Posted: Tue May 17, 2011 12:11 pm
by Seymour Clufley
Regarding the request for ChangeCurrentMapElement(), is the need for that met by the newest additions of PushMapPosition() and PopMapPosition()?
In most cases it probably is. But I want to delete a map element without breaking the ForEach loop, and you can't delete an element that's in PopMapPosition stack.
Why would you want a new element to become current if you are deleting them using a key anyway?
I just want to have a ForEach loop in which a map element can be deleted (by key) without the loop aborting.

Re: More commands for Maps

Posted: Tue May 17, 2011 7:40 pm
by Trond
Why would you want a new element to become current if you are deleting them using a key anyway?
I just want to have a ForEach loop in which a map element can be deleted (by key) without the loop aborting.
It would be easier if you didn't need to delete the element by key, but it can still be done:

Code: Select all

NewMap F.i()
F("apple") = 0
F("pear") = 1
F("orange") = 2
F("plum") = 3
F("grapes") = 4

ForEach F()
  If F() = 3
    Key.s = MapKey(F())
    DeleteMapElement(F(), "plum")
    F(Key)
  Else
    Debug MapKey(F())
  EndIf
Next

Re: More commands for Maps

Posted: Tue May 17, 2011 7:48 pm
by Demivec
Trond wrote:
Why would you want a new element to become current if you are deleting them using a key anyway?
I just want to have a ForEach loop in which a map element can be deleted (by key) without the loop aborting.
It would be easier if you didn't need to delete the element by key, but it can still be done:
@Trond: Your example deletes the current element, thus no need for a key. It's even easier using:

Code: Select all

NewMap f.i()
f("apple") = 0
f("pear") = 1
f("orange") = 2
f("plum") = 3
f("grapes") = 4

ForEach f()
  If f() = 3
    DeleteMapElement(f())
  Else
    Debug MapKey(f())
  EndIf
Next
Here's another variation that delete a non-current element:

Code: Select all

NewMap f.i()
f("apple") = 0
f("pear") = 1
f("orange") = 2
f("plum") = 3
f("grapes") = 4

ForEach f()
  If f() = 3
    PushMapPosition(f())
    DeleteMapElement(f(), "apple") ;deletes from a non-current element and never a current one
    PopMapPosition(f())
  EndIf
Next

ForEach f()
  Debug f()
Next 
And for completeness this version combines the two previous examples and deletes an element whether or not it is the current element:

Code: Select all

NewMap f.i()
f("apple") = 0 ;change this to 3 for a complete test
f("pear") = 1
f("orange") = 2
f("plum") = 3
f("grapes") = 4

ForEach f()
  If f() = 3
    If MapKey(f()) = "apple"
      DeleteMapElement(f())
    Else
      PushMapPosition(f())
      DeleteMapElement(f(), "apple") ;deletes from a non-current element
      PopMapPosition(f())
    EndIf 
  EndIf
Next

ForEach f()
  Debug MapKey(f()) + " : " + Str(f())
Next 

Re: More commands for Maps

Posted: Tue May 17, 2011 8:10 pm
by Trond
@Trond: Your example deletes the current element, thus no need for a key. It's even easier using:
I know that, but he specifically asked for deletion by key. My example works even if not deleting the current element:

Code: Select all

NewMap F.i()
F("apple") = 0
F("pear") = 1
F("orange") = 2
F("plum") = 3
F("grapes") = 4

ForEach F()
  If F() = 3
    Key.s = MapKey(F())
    DeleteMapElement(F(), "apple")
    F(Key)
  Else
    Debug MapKey(F())
  EndIf
Next

Re: More commands for Maps

Posted: Tue May 17, 2011 8:21 pm
by Demivec
Trond wrote:
@Trond: Your example deletes the current element, thus no need for a key. It's even easier using:
I know that, but he specifically asked for deletion by key. My example works even if not deleting the current element:
I just wanted to point out your example does not work when deleting the current element. This is because when you use the key again it recreates the element.

Code: Select all

NewMap f.i()
f("apple") = 0
f("pear") = 1
f("orange") = 2
f("plum") = 3
f("grapes") = 4

ForEach f()
  If f() = 3
    key.s = MapKey(f())
    DeleteMapElement(f(), "plum")
    f(key)
  Else
    Debug MapKey(f())
  EndIf
Next

Debug "----"
ForEach f()
  Debug MapKey(f())
Next

Re: More commands for Maps

Posted: Thu May 19, 2011 12:44 am
by Demivec
Seymour Clufley wrote:PreviousMapElement()
- opposite of NextMapElement()
This request has no benefit. Map elements are not sortable. This means the only reason for using NextMapElement() is IMHO to go through all the elements of a map. If you are going through all the elements of an unsorted list it doesn't make any difference whether you go through them forward or backward. As a consequence that would make this request meaningless.