Got an idea for enhancing PureBasic? New command(s) you'd like to see?
akj
Enthusiast
Posts: 668 Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham
Post
by akj » Mon Nov 16, 2009 12:47 am
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
Posts: 4777 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Mon Nov 16, 2009 1:35 am
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
Posts: 668 Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham
Post
by akj » Mon Nov 16, 2009 2:45 am
@Little John:
"US"
Anthony Jordan
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Mon Nov 16, 2009 4:05 am
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
Posts: 4777 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Mon Nov 16, 2009 6:21 am
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
Posts: 5940 Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany
Post
by freak » Mon Nov 16, 2009 1:24 pm
Who sais a map can contain only strings ?
quidquid Latine dictum sit altum videtur
akj
Enthusiast
Posts: 668 Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham
Post
by akj » Mon Nov 16, 2009 2:23 pm
@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
Posts: 5940 Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany
Post
by freak » Mon Nov 16, 2009 2:34 pm
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