udfStrToMorse (3)
str udfStrToMorse (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrToMorse (strString) ; Version 3.
If strString == "" Then Return ""

strCharsLatin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; 26 upper alpha chars.
strCharsNum = "0123456789" ; 10 numerical chars.
strCharsSpecial = "ÀÅÄÈÉÖÜßÑ.,:;?-_()'=+/@" ; 23 upper special chars. ; Note: CH = "----" ; not handled by this code.
strSpace = " "
strChars = strCharsLatin : strCharsNum : strCharsSpecial : strSpace
Drop (strCharsLatin, strCharsNum, strCharsSpecial)

strDelim = "|"
strMorseLatin = ".-|-...|-.-.|-..|.|..-.|--.|....|..|.---|-.-|.-..|--|-.|---|.--.|--.-|.-.|...|-|..-|...-|.--|-..-|-.--|--.." ; 26 codes.
strMorseNum = "-----|.----|..---|...--|....-|.....|-....|--...|---..|----." ; 10 codes.
strMorseSpecial = ".--.-|.--.-|.-.-|.-..-|..-..|---.|..--|...--..|--.--|.-.-.-|--..--|---...|-.-.-.|..--..|-....-|..--.-|-.--.|-.--.-|.----.|-...-|.-.-.|-..-.|.--.-." ; 23 codes.
arrMorse = Arrayize (strMorseLatin : strDelim : strMorseNum : strDelim : strMorseSpecial : strDelim : strSpace, strDelim) ; 60 codes (59 codes plus one space).
DropWild ("strMorse*")
Drop (strDelim, strSpace)

strSplit = ""
For intI = 1 To 255
   strDelimiter = Num2Char (intI)
   If !StrIndex (strChars, strDelimiter, 1, @FWDSCAN)
      objRegExp = ObjectCreate ("VBScript.RegExp")
      objRegExp.IgnoreCase = @FALSE
      objRegExp.MultiLine = @FALSE
      objRegExp.Global = @TRUE
      objRegExp.Pattern = ""
      strSplit = objRegExp.Replace(strChars, strDelimiter)
      objRegExp.Pattern = "^" : strDelimiter : "|" : strDelimiter : "$"
      arrChars = Arrayize (objRegExp.Replace(strSplit, ""), strDelimiter)
      DropWild ("obj*")
      Break
   EndIf
Next
Terminate (strSplit == "", "Terminated.", "Cannot arrayize charset.")
Drop (strSplit)

objDict = CreateObject ("Scripting.Dictionary")
objDict.CompareMode = 0 ; BinaryCompare, i. e. "That" != "that"

intArrayElements = ArrInfo (arrChars, 1)
intMax = intArrayElements - 1
For intI = 0 To intMax
   objDict.Add(arrChars [intI], arrMorse[intI])
Next
Drop (arrChars)

strString = StrUpper (StrClean (strString, strChars, "", @FALSE, 2)) ; Remove all other unknown chars.
strMorse = ""
intLen = StrLen (strString)
For intI = 1 To intLen
   strMorse = strMorse : objDict.Item(StrSub(strString, intI, 1))
Next

objDict.RemoveAll
Drop (objDict)

Return strMorse
;..........................................................................................................................................
; This UDF "udfStrToMorse" converts a given string into it's equivalent Morse code.
;
; strCharsLatin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; 26 upper alpha chars.
; strCharsNum = "0123456789" ; 10 numerical chars.
; strCharsSpecial = "ÀÅÄÈÉÖÜßÑ.,:;?-_()'=+/@" ; 23 special chars.
; Note: CH = "----" ; not handled by this code.
;
; Detlev Dalitz.20090703
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strString = "SOS"
strMorse = udfStrToMorse (strString) ; strMorse = "...---..."
strMsgTitle = strString
strMsgText = strMorse
Message (strMsgTitle, strMsgText)

strString = "- (  [<{Hello}>] World!  ) ? ."
strMorse = udfStrToMorse (strString) ; strMorse = "-....- -.--.  ......-...-..--- .-----.-..-..-..  -.--.- ..--.. .-.-.-"
strMsgTitle = strString
strMsgText = strMorse
Message (strMsgTitle, strMsgText)

Exit