Joakim Hjalmarsson
Joakim Hjalmarsson

Reputation: 29

Turning strings into emojis using lua

So I am working on a encoding algorithm that changes letters in to emojis, but my algorithm returns nil on some characters, why is this?

local function enc(MSG)
    local resources = {"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","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", "-", "_", "*", "'", "^", "~", "!"}
    local resourcesT = {"😜", "🏀", "🔼", "🚐", "👠", "🌔", "⛹", "🐑", "㊗️", "🍒", "📳", "🛄", "🎮", "💌", "🎣", "☁️", "🍭", "💨", "🏄", "🕢", "🏕", "😽", "🌹", "👜", "🔰", "⛄️","🔯", "↪️", "🐅", "🍑", "❗️", "🐺", "🐀", "🐤", "🔝", "🈷️", "🐰", "🏞", "🚜", "⚫️", "💏", "⭕️", "🍺", "☣", "🏤", "⏯", "👟", "😝", "👝", "🎋", "⌛️", "⌨", "🏳", "🚧", "⏫", "😟", "🚬", "🍧", "🌾", "🛋", "🚒", "🙊", "🦁", "🙈", "😶", "#️⃣", "💥", "🐴", "✖️", "🐎"}
    local finalstring = ""
    local count = 0
    MSG:gsub(".", function(c)
    for i,v in pairs(resources) do
              count = count + 1
               if v == c then
                 finalstring = finalstring .. tostring(resourcesT[count])
                 count = 0
             end
         end
    end)
    return finalstring
end
local Enc = enc("Hello World")
local Thread = io.open("l.lua", "w")
Thread:write(Enc)

Upvotes: 0

Views: 172

Answers (1)

Francisco
Francisco

Reputation: 450

You don't need to keep a track on the count variable, just use the available i variable and it will work fine:

local function enc(MSG)
    local resources = {"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","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", "-", "_", "*", "'", "^", "~", "!"}
    local resourcesT = {"😜", "🏀", "🔼", "🚐", "👠", "🌔", "⛹", "🐑", "㊗️", "🍒", "📳", "🛄", "🎮", "💌", "🎣", "☁️", "🍭", "💨", "🏄", "🕢", "🏕", "😽", "🌹", "👜", "🔰", "⛄️","🔯", "↪️", "🐅", "🍑", "❗️", "🐺", "🐀", "🐤", "🔝", "🈷️", "🐰", "🏞", "🚜", "⚫️", "💏", "⭕️", "🍺", "☣", "🏤", "⏯", "👟", "😝", "👝", "🎋", "⌛️", "⌨", "🏳", "🚧", "⏫", "😟", "🚬", "🍧", "🌾", "🛋", "🚒", "🙊", "🦁", "🙈", "😶", "#️⃣", "💥", "🐴", "✖️", "🐎"}
    local finalstring = ""
    MSG:gsub(".", function(c)
        for i, v in ipairs(resources) do
                if v == c then
                    finalstring = finalstring .. tostring(resourcesT[i])
                end
            end
    end)
    return finalstring
end
local Enc = enc("Hello World") -- 🐤👠🛄🛄🎣🏳👝🎣💨🛄🚐

You can see this code working here.

Upvotes: 1

Related Questions