Request for Enhancement to MapKey()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Request for Enhancement to MapKey()

Post 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)
Anthony Jordan
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Request for Enhancement to MapKey()

Post 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
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Request for Enhancement to MapKey()

Post by akj »

@Little John:

"US"
Anthony Jordan
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Request for Enhancement to MapKey()

Post 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.
oh... and have a nice day.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Request for Enhancement to MapKey()

Post 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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Request for Enhancement to MapKey()

Post by freak »

Who sais a map can contain only strings ?
quidquid Latine dictum sit altum videtur
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Request for Enhancement to MapKey()

Post 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.
Anthony Jordan
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Request for Enhancement to MapKey()

Post 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.
quidquid Latine dictum sit altum videtur
Post Reply