Page 1 of 1
Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 12:47 am
by akj
Given:
Code: Select all
NewMap Country.s()
Country("US") = "United States"
Country("FR") = "France"
Country("GE") = "Germany"
Please enhance MapKey() so that
Code: Select all
Debug MapKey(Country(), "France") ; The result is "FR"
is functionally equivalent to
Code: Select all
ForEach Country()
If Country()="France"
Debug MapKey(Country())
Break
EndIf
Next
It would also be useful to have a case-sensitivity flag along the lines of:
Code: Select all
Debug MapKey(Country(), "France", #PB_Map_NoCase)
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 1:35 am
by Little John
The elements of a Map() are not always unique. For instance, we can have
Code: Select all
NewMap Country.s()
Country("US") = "democracy"
Country("FR") = "democracy"
Country("GE") = "democracy"
According to your suggestion, what should be the result of
Code: Select all
Debug MapKey(Country(), "democracy")
Regards, Little John
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 2:45 am
by akj
@Little John:
"US"
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 4:05 am
by Kaeru Gaman
why not "FR"? it's first in alphabeth...
since Maps have no indices, the order should be undefined, so you cannot say it should be the element you declared first.
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 6:21 am
by Little John
akj wrote:@Little John:
"US"
Why? IMHO the correct result would be "US", "FR"
and "GE". But MapKey() only returns one key.
Regards, Little John
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 1:24 pm
by freak
Who sais a map can contain only strings ?
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 2:23 pm
by akj
@Freak:
I don't think anyone was saying that maps can contain only strings.
The example I chose to illustrate my request just happened to be string based.
My example is generalisable to any map type including structures.
Re: Request for Enhancement to MapKey()
Posted: Mon Nov 16, 2009 2:34 pm
by freak
The MapKey() function needs to have a fixed type for its arguments (PB has no polymorphic functions), so how would you handle strings and structures then ?
Besides, this kind of reverse lookup is awfully slow and as you have shown above it can be easily done in just a few lines, so i don't think there is a need for a built in function.