MapKeysToString(), MapKeysToArray(), MapKeysToList()

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

MapKeysToString(), MapKeysToArray(), MapKeysToList()

Post 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)
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: MapKeysToString(), MapKeysToArray(), MapKeysToList()

Post by Trond »

Why would you need this?
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

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

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