Bolodefchoco_LUAXML

[Function] Morse

Oct 9th, 2015
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.03 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 09/10/2015
  3. --Last update: 25/11/2016
  4. --[[ Notes:
  5.     morse.to
  6.         Does:
  7.             Transforma str em morse
  8.         Args:
  9.             str --> Texto
  10.     morse.from
  11.         Does:
  12.             Transforma code (morse) em texto
  13.         Args:
  14.             code --> Morse
  15. ]]--
  16.  
  17. morse = {}
  18. morse.code = {a=".-",b="-...",c="-.-.",d="-..",e=".",f="..-.",g="--.",h="....",i="..",j=".---",k="-.-",l=".-..",m="--",n="-.",o="---",p=".--.",q="--.-",r=".-.",s="...",t="-",u="..-",v="...-",w=".--",x="-..-",y="-.--",z="--..",[1]=".----",[2]="..---",[3]="...--",[4]="....-",[5]=".....",[6]="-....",[7]="--...",[8]="---..",[9]="----.",[0]="-----"}
  19. morse.to = function(str)
  20.     str = str:lower()
  21.     str = str:gsub(" +"," / ")
  22.     for i,v in next,morse.code do
  23.         str = str:gsub(tostring(i),v.." ")
  24.     end
  25.     return str
  26. end
  27. morse.from = function(code)
  28.     code = code:gsub("\n+"," ")
  29.     local c = ""
  30.     for k in code:gmatch("[^%s]+") do
  31.         for i,j in next,morse.code do
  32.             if k == j then
  33.                 c = c .. i
  34.             elseif k == "/" then
  35.                 c = c .. " "
  36.                 break
  37.             end
  38.         end
  39.     end
  40.     return c
  41. end
Advertisement
Add Comment
Please, Sign In to add comment