More commands for Maps
-
- Addict
- Posts: 1264
- Joined: Wed Feb 28, 2007 9:13 am
- Location: London
More commands for Maps
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.
- 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.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Re: More commands for Maps
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):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.
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
-
- Addict
- Posts: 1264
- Joined: Wed Feb 28, 2007 9:13 am
- Location: London
Re: More commands for Maps
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.Regarding the request for ChangeCurrentMapElement(), is the need for that met by the newest additions of PushMapPosition() and PopMapPosition()?
I just want to have a ForEach loop in which a map element can be deleted (by key) without the loop aborting.Why would you want a new element to become current if you are deleting them using a key anyway?
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Re: More commands for Maps
It would be easier if you didn't need to delete the element by key, but it can still be done:I just want to have a ForEach loop in which a map element can be deleted (by key) without the loop aborting.Why would you want a new element to become current if you are deleting them using a key anyway?
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
@Trond: Your example deletes the current element, thus no need for a key. It's even easier using:Trond wrote:I just want to have a ForEach loop in which a map element can be deleted (by key) without the loop aborting.Why would you want a new element to become current if you are deleting them using a key anyway?
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
DeleteMapElement(f())
Else
Debug MapKey(f())
EndIf
Next
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
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
I know that, but he specifically asked for deletion by key. My example works even if not deleting the current element:@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
Key.s = MapKey(F())
DeleteMapElement(F(), "apple")
F(Key)
Else
Debug MapKey(F())
EndIf
Next
Re: More commands for Maps
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.Trond wrote:I know that, but he specifically asked for deletion by key. My example works even if not deleting the current element:@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
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
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.Seymour Clufley wrote:PreviousMapElement()
- opposite of NextMapElement()