I am trying to use string patterns to detect whether a character is punctuation (-, !, ; etc), a alphabetical letter (a,b,c etc), or a whitespace (space bar).
The problem is that the script says that “a” is punctuation not a letter.
for w in string.gmatch(lowerInput, "[%a%s%p]") do -- W = Letter + Number | S = Whitespace
if w == " " then
assembleEncryptedString(" ")
else
if string.gmatch(w,"%p") then
print("Punctuation - "..w)
assembleEncryptedString(w)
else
print("Letter - "..w)
local index = table.find(alphabet,w)
local reversedIndex = 27-index
local reversedLetter = alphabet[reversedIndex]
assembleEncryptedString(reversedLetter)
end
end
end
local function getLettersOnly(String)
local withoutNumbers = string.gsub(String, "[%d+]", "")
return string.gsub(withoutNumbers, "[%p+]", "") -- removes puncuation.
end
local function getNumbersOnly(String)
local withoutLetters = string.gsub(String, "[%a+]", "")
return string.gsub(withoutLetters, "[%p+]", "") -- removes puncuation.
end
local function getPunctuationOnly(String)
local withoutLetters = string.gsub(String, "[%a+]", "")
return string.gsub(withoutLetters, "[%d+]", "") -- removes numbers.
end
In your conditional that checks if the input is punctuation, you have accidentally written string.gmatch instead of string.match, which subsequently returns a truthy value regardless of whether or not it gets any hits, so the input is being mistakenly considered as punctuation.