• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Utility function for 'a' & 'an'

Codex NG

Recurrent Flamer
Joined
Jul 24, 2015
Messages
2,994
Solutions
12
Reaction score
1,657
Might not seem important, I like to sometimes write little utility functions although I don't always use them.

This is a simple function (if it doesn't already exist) which will allow you to state where the word a (yes a is a word) or an will be used in a sentence.

Unfortunately this only pertains to english, I am sure you can adapt it to your native language based on its rules of grammar.
Code:
--[[
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
Code:
function aOrAn(word)
    local vowels = {'a', 'e', 'i', 'o', 'u', 'x'}
    return isInArray(vowels, word:sub(1, 1):lower()) and ' an '..word or ' a '..word
end

If you don't have an isInArray function in your distro or whatever you are working on, you can always use this function definition below.

Code:
function isInArray(a, x)
    for k, v in pairs(a) do
        if v == x then
            return true
        end
    end
    return false
end

So how can this be used.. Lets take a look at an example
Code:
function aOrAn(word)
    local vowels = {'a', 'e', 'i', 'o', 'u', 'x'}
    return isInArray(vowels, word:sub(1, 1):lower()) and ' an '..word or ' a '..word
end

local name = 'elephant'
print("You have summoned"..aOrAn(name)..".")
-- output
You have summoned an elephant.

Lets try another
Code:
local name = 'deer'
print("You have summoned"..aOrAn(name)..".")
-- output
You have summoned a deer.

One more :p
Code:
local name = 'Xray'
print("You went and got"..aOrAn(name)..".")
-- output
You went and got an Xray.

Its not perfect by any means, but its definitely helpful to have functions which can simplify things at times.
 
Last edited:
Code:
function aOrAn(word)
    return word:gsub("^.", function(char) return ("aeiou"):find(char:lower()) and ('an ' .. char) or ('a ' .. char) end)
end

Better (wont consider characters that are not letters in the beginning of the word like spaces etc)
Code:
function aOrAn(word)
    return word:gsub("^(.-)(%a)", function(_, char) return _ .. (("aeiou"):find(char:lower()) and "an " or "a ") .. char end)
end

Not using gsub:
Code:
function aOrAn(word)
    return ("aeiou"):find(word:sub(1, 1):lower()) and "an " .. word or "a " .. word
end
Let it be clear that i'm posting this for people to see another way to see how it could be done (without is in array and using gsub)
 
Last edited:
Back
Top