Code: Select all
NewMap Country.s()
Country("US") = "United States"
Country("FR") = "France"
Country("GE") = "Germany"
ForEach Country()
Debug MapKey(Country())
Next

Code: Select all
NewMap Country.s()
Country("US") = "United States"
Country("FR") = "France"
Country("GE") = "Germany"
ForEach Country()
Debug MapKey(Country())
Next
Code: Select all
NewMap Country.s()
Country("US") = "United States"
Country("FR") = "France"
Country("GE") = "Germany"
Country("FR")
Debug MapKey(Country())
Code: Select all
NewMap Country.s()
Country("US") = "United States"
Country("FR") = "France"
Country("GE") = "Germany"
ForEach Country()
If Country() = "France"
Debug MapKey(Country())
EndIf
Next
Code: Select all
NewMap CountryByShort.s()
NewMap ShortByCountry.s()
CountryByShort("US") = "United States"
CountryByShort("FR") = "France"
CountryByShort("GE") = "Germany"
ForEach CountryByShort()
ShortByCountry(CountryByShort()) = MapKey(CountryByShort())
Next
Debug CountryByShort("FR")
Debug ShortByCountry("France")

Hahahahahaha!!! If I know the key then what is my problem? Hahahaha!!!infratec wrote:MapKey() returns the key of the current selected element.
So you have to select it.
But then you know already the key.![]()
I knew thatinfratec wrote: Or:Or you need an additional 'reverse' Map:Code: Select all
...Code: Select all
...
Code: Select all
ForEach Country()
If Country() = "France"
Debug MapKey(Country())
EndIf
NextCode: Select all
MapKey("France")Code: Select all
CompilerIf Defined (ChangeCurrentMapElement, #PB_Function) = #False
Macro ChangeCurrentMapElement (MyMap, NewMapElement)
FindMapElement (MyMap, PeekS (PeekI (NewMapElement - SizeOf (Integer))))
EndMacro
CompilerEndIf

There are a few complicated workarounds. I had a hope that MapKey() would be a little more useful/helpful and I did't have to search all elements myself. Anyway, it doesn't matter. Thank you both for you replays! I really appreciate them!Josh wrote:If you are looking for a reverse function to search for the value of a map entry, you will have no choice but to go through the map. How else could it be? The content of a map element can also be a structure.
Maybe, this can help you. For Maps there is no equivalent to the list command ChangeCurrentElement(). You can use the following workaround:Code: Select all
CompilerIf Defined (ChangeCurrentMapElement, #PB_Function) = #False Macro ChangeCurrentMapElement (MyMap, NewMapElement) FindMapElement (MyMap, PeekS (PeekI (NewMapElement - SizeOf (Integer)))) EndMacro CompilerEndIf

the key is unique, the value is not. thats why.doctorized wrote:I don't get it, why do we have to loop all elements to find the one we need?
MapKey() does exactly what it should do, which is returning the key. if you want to get a specific value, you have to search for it, or create two maps and cross reference them.doctorized wrote:I had a hope that MapKey() would be a little more useful/helpful

In my case, values are unique as the are coming from enumeration.nco2k wrote:the key is unique, the value is not. thats why.
I want a connection between strings and numbers coming from enumeration. The numbers are stored in a database (I use the numbers for smaller db). Strings are used to represent the numbers in a list icon gadget. So, I need both direction connectivity. The string gives a number to be stored, the number read is used to get the string to show. Any suggestion other than the map? With the foreach loop I am ok right now. Anything else better than this?nco2k wrote:you are using maps wrong however. the key is something that you know, and the value is something that you dont know, but want to access quickly.

Code: Select all
UseSQLiteDatabase()
db = OpenDatabase(#PB_Any,":memory:","","",#PB_Database_SQLite)
; etc...
If you don't want to build it as a pure database solution, then I would write the strings into a map and save the pointer to the map entry in the database. How you get the Mapkey from the pointer I already described above. This way you save the whole spell with values, enumeration, search, etc.doctorized wrote:I want a connection between strings and numbers coming from enumeration. The numbers are stored in a database (I use the numbers for smaller db). Strings are used to represent the numbers in a list icon gadget. So, I need both direction connectivity.
it doesnt matter. the key has to be unique for the map to work properly. the value on the other hand is just a data container. the map doesnt care what you do with it. some other languages may offer what you want, but they still have to search for the item under the hood. so you arent gaining anything.doctorized wrote:In my case, values are unique
If both the key and the value are unique with respect to themselves and to each other you can store them in the same map.doctorized wrote:In my case, values are unique as the are coming from enumeration.nco2k wrote:the key is unique, the value is not. thats why.
I want a connection between strings and numbers coming from enumeration. The numbers are stored in a database (I use the numbers for smaller db). Strings are used to represent the numbers in a list icon gadget. So, I need both direction connectivity. The string gives a number to be stored, the number read is used to get the string to show. Any suggestion other than the map? With the foreach loop I am ok right now. Anything else better than this?nco2k wrote:you are using maps wrong however. the key is something that you know, and the value is something that you dont know, but want to access quickly.
Code: Select all
; DEF: When to use array|list|map?
; Map: Unique list, no order. Text defines elements.
; Ex. Menu items, except watch for sub-menus with same names.
; MapSize(map()) returns base 1 count.
; List: Ordered list. Usually small and unknown size at creation.
; Ex. Retrieving a table's contents without knowing count.
; Or appending to existing list without care to size.
; ListSize(ll()) returns base 1 count.
; Array: When speed or list size demand.
; ArraySize(ar()) returns base 0 count.
which could introduce potential problems in the future, if your rules ever change. its better to keep them separate, and reference them through pointers.Demivec wrote:If both the key and the value are unique with respect to themselves and to each other you can store them in the same map.