Android Question [Solved] Map.GetDefault Problem

Similar threads

B4A Code Snippet Password generator Sub
B4A Question Sub that returns 'a' or 'an'
B4A Code Snippet ShuffleList
B4A Library WaveSideBar
B4A Question Map Collection - The most useful collection...

mw71

Active Member
Licensed User
Hi

i create and fill a Map with normal and special charters ("Translate Table")
B4X:
    mReplace = CreateMap("À":"A","Á":"A","Â":"A","Ã":"A","Ä":"AE","Å":"A","Ā":"A","Ă":"A","Ą":"A","Ǎ":"A","Ǻ":"A", _
    "Æ":"AE","Ǽ":"AE","Ç":"C","Ć":"C","Ĉ":"C","Ċ":"C","Č":"C", "Ð":"D","Ď":"D","Đ":"D", _
    "È":"E","É":"E","Ê":"E","Ë":"E","Ē":"E","Ĕ":"E","Ė":"E","Ę":"E","Ě":"E", _
    "Ĝ":"G","Ġ":"G","Ģ":"G","Ğ":"G","Ĥ":"H","Ħ":"H", _
    "Ì":"I","Í":"I","Î":"I","Ï":"I","Ĩ":"I","Ī":"I","Ĭ":"I","Į":"I","İ":"I","Ǐ":"I", _
    "IJ":"J","Ĵ":"J","Ķ":"K","Ĺ":"L","Ļ":"L","Ľ":"L","Ŀ":"L","Ł":"L", _
    "Ń":"N","Ņ":"N","Ň":"N","Ñ":"N", _
    "Ò":"O","Ó":"O","Ô":"O","Õ":"O","Ö":"OE","Ø":"O","Ō":"O","Ŏ":"O","Ő":"O","Ơ":"O","Ǒ":"O","Ǿ":"O", _
    "Ŕ":"R","Ŗ":"R","Ř":"R","Ś":"S","Ŝ":"S","Ş":"S","Š":"S","Ţ":"T","Ť":"T","Ŧ":"T", _
    "Ũ":"U","Ū":"U","Ŭ":"U","Ů":"U","Ű":"U","Ų":"U","Ù":"U","Ú":"U","Û":"U","Ü":"UE","Ư":"U","Ǔ":"U","Ǖ":"U","Ǘ":"U","Ǚ":"U","Ǜ":"U", _
    "ß":"ss","Ŵ":"W","Ý":"Y","Ŷ":"Y","Ÿ":"Y","Ź":"Z","Ż":"Z","Ž":"Z", _
    "à":"a","á":"a","â":"a","ã":"a","ä":"a","å":"a","ā":"ae","ă":"a","ą":"a","ǎ":"a","ǻ":"a", _
    "æ":"ae","œ":"ce","ǽ":"ae","ć":"c","ĉ":"c","ċ":"c","č":"c","ç":"c","ď":"d","đ":"d", _
    "è":"e","é":"e","ê":"e","ë":"e","ē":"e","ĕ":"e","ė":"e","ę":"e","ě":"e","ſ":"f","ƒ":"f", _
    "ĝ":"g","ğ":"g","ġ":"g","ģ":"g","ĥ":"h","ħ":"h", _
    "ì":"i","í":"i","î":"i","ï":"i","ı":"i","į":"i","ĭ":"i","ī":"i","ĩ":"i","ǐ":"i", _
    "ij":"j","ĵ":"j","ķ":"k","ŀ":"l","ľ":"l","ļ":"l","ĺ":"l","ł":"l","ñ":"n","ň":"n","ņ":"n","ń":"n","ʼn":"n", _
    "ò":"o","ó":"o","ô":"o","õ":"o","ö":"oe","ø":"o","ǿ":"o","ō":"o","ŏ":"o","ő":"o","ơ":"o","ǒ":"o", _
    "ŕ":"r","ŗ":"r","ř":"r","ś":"s","ŝ":"s","ş":"s","š":"s","ţ":"t","ť":"t","ŧ":"t", _
    "ù":"u","ú":"u","û":"u","ü":"ue","ū":"u","ũ":"u","ŭ":"u","ů":"u","ű":"u","ų":"u","ǜ":"u","ǚ":"u","ǘ":"u","ǖ":"u","ǔ":"u","ư":"u", _
    "ŵ":"w","ŷ":"y","ý":"y","ÿ":"y","ź":"z","ż":"z","ž":"z", _
    "Œ":"CE")
   
    For x=32 To 126 Step 1
        mReplace.Put(Chr(x),Chr(x))
    Next
now i Test but only charters from the Create Map part will be "translate"

e.g. with X (in the Code c is a string Variable), ASCII (Chr) Code is 88 (Log with the ASC funkt.), but with
B4X:
mReplace.GetDefault(c,"_")
, result is _ ("Default Value").

Whats Wrong?,

The Map contain all (the Create Map and the For Next
With the Key/Value from Create Map part it works
 
Last edited:

emexes

Well-Known Member
Licensed User
Can't test right now, but...I vaguely remember some trap about initialized lists being constant, perhaps the same is true of maps. Although if that were the case, you'd think that the .Puts would tell you about it.

Confirm that the ASCII printable characters are being added, eg:
B4X:
Log("Before = " & mReplace.Size)
For x=32 To 126 Step 1
    mReplace.Put(Chr(x),Chr(x))
Next
Log("After = " & mReplace.Size)
ps I like that you didn't add Chr(127) ;-)
 

emexes

Well-Known Member
Licensed User
I was mulling over lunch about that impressive conversion map that you've made, which reminded me of:

https://www.compart.com/en/unicode/U+00E9

which links at the bottom to a spreadsheet-importable complete list of Unicode letters:

ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt

Also, I was looking at how other people had done similar conversions, and I do like the one mentioned in:

https://stackoverflow.com/questions...nted-characters-to-pure-ascii-without-accents
B4X:
import re

def remove_accents(string):
   if type(string) is not unicode:
       string = unicode(string, encoding='utf-8')

   string = re.sub(u"[àáâãäå]", 'a', string)
   string = re.sub(u"[èéêë]", 'e', string)
   string = re.sub(u"[ìíîï]", 'i', string)
   string = re.sub(u"[òóôõö]", 'o', string)
   string = re.sub(u"[ùúûü]", 'u', string)
   string = re.sub(u"[ýÿ]", 'y', string)

   return string
which I appreciate is not BASIC but the gist of it is that presenting the conversions in that many-to-one style is more readable and less prone to missing stuff (btw I did note that you'd grouped letters by line - nice touch!). You could create a list of those many-to-one conversions (for 26 letters plus the oddball stragglers) and then use that list to construct a translation Map of the same format you have now.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
I vaguely remember some trap about initialized lists being constant, perhaps the same is true of maps
The map is not read only.

The problem happens because Chr(x) returns a Char value instead of a string. The keys type must match.

Fix:
B4X:
For x=32 To 126 Step 1
   mReplace.Put("" & Chr(x),Chr(x))
Next
 

mw71

Active Member
Licensed User
thanks for Answer and Links (nice information, i need more Time to read ;))

now it Works:)
 
Top