Advertisement
zippy36jr

morse

Nov 28th, 2023 (edited)
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. local alphabet = {
  2.     ["a"] = ".-",
  3.     ["b"] = "-...",
  4.     ["c"] = "-.-.",
  5.     ["d"] = "-..",
  6.     ["e"] = ".",
  7.     ["f"] = "..-.",
  8.     ["g"] = "--.",
  9.     ["h"] = "....",
  10.     ["i"] = "..",
  11.     ["j"] = ".---",
  12.     ["k"] = "-.-",
  13.     ["l"] = ".-..",
  14.     ["m"] = "--",
  15.     ["n"] = "-.",
  16.     ["o"] = "---",
  17.     ["p"] = ".--.",
  18.     ["q"] = "--.-",
  19.     ["r"] = ".-.",
  20.     ["s"] = "...",
  21.     ["t"] = "-",
  22.     ["u"] = "..-",
  23.     ["v"] = "...-",
  24.     ["w"] = ".--",
  25.     ["x"] = "-.--",
  26.     ["y"] = "-.--",
  27.     ["z"] = "--.."
  28. }
  29.  
  30. function table.hasKey(table, key)
  31.     for k, _ in pairs(table) do
  32.       if k == key then
  33.         return true
  34.       end
  35.     end
  36.     return false
  37.   end
  38.  
  39. function convertTxtToMorse(text)
  40.     local result = {}
  41.  
  42.     for letter in string.lower(text):gmatch(".") do
  43.         if (letter == " ") then
  44.             table.insert(result, " ")
  45.             print("space")
  46.         elseif (not table.hasKey(alphabet, letter)) then
  47.             print("not a letter")
  48.         else
  49.         table.insert(result, alphabet[letter])
  50.         print(alphabet[letter])
  51.         end
  52.     end
  53.  
  54.     return result
  55. end
  56.  
  57. convertTxtToMorse("test text 1234")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement