Page 1 of 1

MapKeysToString(), MapKeysToArray(), MapKeysToList()

Posted: Tue Sep 14, 2010 5:02 am
by Seymour Clufley
I know these functions are trivial, but I often need them in my programs. I've written my own procedures but I'm sure native commands would be faster. Plus, my procedures can only take one kind of map so there's a lot of duplication there.

Code: Select all

stringarray.s = MapKeysToString(mp(),"|") ; the second parameter is a delimiter for the string array

NewList ls.s()
MapKeysToList(mp(),ls(),#PB_MapKeys_Add)

Dim arr.s(0)
MapKeysToArray(mp(),arr(),#PB_MapKeys_OnlyThese)
#PB_MapKeys_Add - the map's keys would be added to the list or array (array would be redimmed to the needed size)

#PB_MapKeys_OnlyThese - the list/array would be emptied and the map's keys added to it (array would be dimmed to the needed size)

Re: MapKeysToString(), MapKeysToArray(), MapKeysToList()

Posted: Tue Sep 14, 2010 9:01 am
by Trond
Why would you need this?

Re: MapKeysToString(), MapKeysToArray(), MapKeysToList()

Posted: Tue Sep 14, 2010 10:50 am
by STARGĂ…TE
These functions are too specific.
You'd only work with string lists and string arrays.
And probably most users have no use for it.
I think a macro should be enough here, they work with all maps:

for example:

Code: Select all

#PB_MapKeys_Add = 0
#PB_MapKeys_OnlyThese = 1

Macro MapKeysToList(MapName, ListName, Flags=#PB_MapKeys_Add)
  If Flags = #PB_MapKeys_OnlyThese
    ClearList(ListName)
  EndIf
  ForEach MapName
    AddElement(ListName)
    ListName = MapKey(MapName)
  Next
EndMacro

NewMap Test.i()
NewList Test2.s()

Test("A") = 1
Test("B") = 2

MapKeysToList(Test(), Test2())

ForEach Test2()
  Debug Test2()
Next
the other two work in a similar

Re: MapKeysToString(), MapKeysToArray(), MapKeysToList()

Posted: Tue Sep 14, 2010 11:25 am
by Seymour Clufley
Trond wrote:Why would you need this?
I need it all the time.

For example, if you're adding elements to a group and you don't want any duplicates, the fastest way is to use a map. But if you then want to sort the elements in some way, you need to transfer them to a list.