More commands for Maps

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

More commands for Maps

Post 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.
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."
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: More commands for Maps

Post 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()?
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: More commands for Maps

Post 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.
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."
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: More commands for Maps

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: More commands for Maps

Post 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 
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: More commands for Maps

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: More commands for Maps

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: More commands for Maps

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