Jump to content




[Lua][Help] String pattern matching


  • You cannot reply to this topic
8 replies to this topic

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 17 September 2012 - 03:55 PM

I'm trying to make a game of Hangman. Problem is, I do not know how I am going to have it detect any letters the user types. I want to be able to add a word in, and have it be cut apart into separate letters, and have everyone guess it. I know that I need to use something like string.match, or string.gmatch, but the only tutorials i found separate whole words out. To get the letters of a word into a table would help a lot. My reference material was here: http://lua-users.org...atternsTutorial

Edit: I also need something like this for my upcoming printing API. I'm trying to make patterns with letters in a string.

Edited by Cranium, 17 September 2012 - 03:58 PM.


#2 GopherAtl

  • Members
  • 888 posts

Posted 17 September 2012 - 04:17 PM

try this to make an array of the letters in the word...
local word="hangman"
local letters={}
string.gsub(word,"%a",function(letter) letters[#letters+1]=letter end)

you can also use string.sub to pull letters out..

letters={}
for i=1,#letters do
  letters[i]=string.sub(letters,i,i)
end


#3 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 17 September 2012 - 04:19 PM

well this is probably an bad workaround but try this:
function seprate(input)
local chars = {}
for x=1,string.len(input) do
table.insert(chars,string.sub(input,x,x))
end
return chars
end


#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 17 September 2012 - 04:24 PM

Thanks for the help, guys. Although I would like to know how you got to those ideas. For example, why string.sub? I am self taught, and many things are still eluding me with Lua.

#5 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 17 September 2012 - 04:30 PM

i also taught lua to myself and at the point that i did that code (its quite old) i didn't knew much about string functions so that was the only thing i knew how to use

#6 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 17 September 2012 - 04:33 PM

View PostGopherAtl, on 17 September 2012 - 04:17 PM, said:

try this to make an array of the letters in the word...
local word="hangman"
local letters={}
string.gsub(word,"%a",function(letter) letters[#letters+1]=letter end)

you can also use string.sub to pull letters out..

letters={}
for i=1,#letters do
  letters[i]=string.sub(letters,i,i)
end

why are you editing my resolution into yours ?

#7 GopherAtl

  • Members
  • 888 posts

Posted 17 September 2012 - 04:36 PM

I ws already editing while you were posting :)/>

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 17 September 2012 - 04:41 PM

Another version:
local word = "some word"
local letters = {}
for letter in string.gmatch(word, ".") do
  table.insert(letters, letter)
end
So many options. Lua is great! :)/>

#9 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 17 September 2012 - 04:47 PM

View PostMysticT, on 17 September 2012 - 04:41 PM, said:

Another version:
local word = "some word"
local letters = {}
for letter in string.gmatch(word, ".") do
  table.insert(letters, letter)
end
So many options. Lua is great! :D/>
i call python :)/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users