Page 1 of 1

Next character in the alphabet

Posted: Sun Jan 30, 2011 3:20 pm
by YuShin
Hi,

I have a (very) basic question about Lua. I would like to get the next (or previous) of a given letter.
I have been looking around, but haven't found any answer. Is it possible?

Here is what i want to do :
I have a list of game names in an array, and a "currentGame" variable that indexes one of the games. I get the first letter of the name with something like "firstLetter = string.sub(gameName, 1, 1)", but then how do i get the ascii code (or equivalent) of this letter? With such code i could then get the next letter with "nextLetter = '\'..\code, then look in my array for a game starting with this letter.

For example, with a list like "Aero figters, altered beast, arkanoid, batsugun", and the current game being altered beast, i would like to get letter B.

I hope my english is good enough for you to understand :p

Thank you.

Re: Next character in the alphabet

Posted: Sun Jan 30, 2011 3:26 pm
by thelinx

Code: Select all

function next_ascii(letter)
  local ascii_code = string.byte(letter)
  local next_ascii_code = ascii_code + 1
  return string.char(next_ascii_code)
end
or, this shorter version:

Code: Select all

function next_ascii(letter)
  return string.char(string.byte(letter)+1)
end

Re: Next character in the alphabet

Posted: Sun Jan 30, 2011 3:28 pm
by YuShin
Thank you,
I somehow failed to find the function that does like... exactly what i want :ehem: