How to change letter to number A=1, B=2?

Is there any easy way to change a letter (from a string) into the corresponding number, such as a=1, b=2, c=3…y=25, z=26 ?

I know I could make a really long if/ifelse that checks for every letter then changes string to specific number, but I feel like this is got to be something that can be done easier.  

Welp, I drafted this up, let me know if there is an easier way. 

local function letterToNumber(letter) local alphabet = {"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"} local num = 0 for i = 1, 26 do if ( alphabet[i] == letter) then num = i break end end return num end

Hi.

These values should be in order on any sane platform, so you can turn a into a byte, do the same with your current letter, then find the difference. An example iterating over the alphabet:

local alphabet = "abcdefghijklmnopqrstuvwxyz" local A = ("a"):byte() for char in alphabet:gmatch(".") do print(char, char:byte() - A + 1) end

That should be okay for the basic character set, but might need some work if you need more than that.

concur: string.byte

also see string.char (in case you need to go the other way too)

All of the above and many other ways.

Handle upper or lowercase with this where ‘c’ is the character you want a value for:

number = string.byte(string.upper( c )) - 64

 Is this a question or an answer?  I think it is the latter, but I’m just curious.

A simple solution that provides the ordinal value of A-Z or a-z.

Welp, I drafted this up, let me know if there is an easier way. 

local function letterToNumber(letter) local alphabet = {"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"} local num = 0 for i = 1, 26 do if ( alphabet[i] == letter) then num = i break end end return num end

Hi.

These values should be in order on any sane platform, so you can turn a into a byte, do the same with your current letter, then find the difference. An example iterating over the alphabet:

local alphabet = "abcdefghijklmnopqrstuvwxyz" local A = ("a"):byte() for char in alphabet:gmatch(".") do print(char, char:byte() - A + 1) end

That should be okay for the basic character set, but might need some work if you need more than that.

concur: string.byte

also see string.char (in case you need to go the other way too)

All of the above and many other ways.

Handle upper or lowercase with this where ‘c’ is the character you want a value for:

number = string.byte(string.upper( c )) - 64

 Is this a question or an answer?  I think it is the latter, but I’m just curious.

A simple solution that provides the ordinal value of A-Z or a-z.